문제

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);
}
도움이 되었습니까?

해결책

You can use this query:

NameObject = context.Names.Where(n => NamesList.Contains(n.PersonName)).ToList();
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top