Search code examples
linqentity-frameworklinq-to-entities

LINQ to compare item in one list to any item in another


I wonder, if someone could help me...

Is there a LINQ query that will return a bool, if any item from one IList<> is contained int another IList<>.

These IList<>'s are object and I need to compare one a single property of the object, the "Name" property in this case?

Is there a LINQ query that can do this? If so could someone show me the correct implementation?

Thank you


Solution

  • Well you could project both lists:

    if (list1.Select(x => x.Name)
             .Intersect(list2.Select(x => x.Name))
             .Any())
    

    Is that what you're after?