Search code examples
c#linqlinq-to-entities

Matching 2 lists in Linq to Entities


I am writing an expression to output a filtered entity List based on the parameters. One condition I have to check is whether all entries in an input array parameter are present in a related entity. first I tried:

((inpArray
     .Intersect(x.B.Select(b=>b.CodeID))
     .Count()==inpArray.Count()) 
|| (inpArray.Count() == 0))

This gives me an exception saying comparable types need to be used in dbIntersect. So I tried converting both to Lists like below:

 ((inpList
         .Intersect(x.B.Select(b=>b.CodeID))
         .ToList()
         .Count()==inpList.Count()) 
    || (inpList.Count() == 0))

Now the error is

DbExpressionBinding requires an input expression with a collection ResultType.

How to best handle the problem?

Many thanks.

EDIT the above code is part of a predicate argument that I need to pass to a service


Solution

  • Try this:

    !inpArray.Except(x.B.Select(b=>b.CodeID)).Any()