Call an object from a List and replace it with another

0

I need to call a specific object through a list to save it in another, I want to do it with the Id attribute, create a foreach like that but it does not return anything even the one that is marked, and I also need that from that Recommended list to be passed to a list of views and be deleted from recommended, I do not know if I'm missing something else for this to work.

foreach (Pelicula A in Recomendadas){
    if (A.Id.Equals(1)){
        Recomendadas.Remove(A)
        break;
    }
}
    
asked by Leooo Queue 25.07.2018 в 20:19
source

1 answer

1

Since you are using lists, read a little about lambda expressions, to find a specific object from the list, you only need to use the FirstOrDefault property like this:

var objeto = Recomendadas.FirstOrDefault(x=>x.Id == tuId);

This takes the specific object from the list that you can then store in the object you want.

    
answered by 25.07.2018 в 22:06