Shwrk
Shwrk

Reputation: 173

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

Upvotes: 0

Views: 158

Answers (2)

Enigmativity
Enigmativity

Reputation: 117084

Try this:

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

Upvotes: 1

SBFrancies
SBFrancies

Reputation: 4240

Try separating out the list generation:

var list1 = inpArray.ToList();
var list2 = x.B.Select(b => b.CodeID).ToList();


if(list1.Count == 0 || list1.Intersect(list2).Count() == list1.Count)
{
    //Your code
}

Upvotes: 0

Related Questions