Question

I have a list of names in NameList list .

I want to filter it and one more list of objects "NameObject" . i able to achieve in the following , but I want to avoid for loop , Is there any better way of achieving this.

foreach (string name in NamesList)
{                               
    var find = context.Names.Single(x => x.PersonName == name);
    NameObject.Add(find);
}
Was it helpful?

Solution

You can use this query:

NameObject = context.Names.Where(n => NamesList.Contains(n.PersonName)).ToList();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top