Geoffrey
Geoffrey

Reputation: 23

How to implement an 'in' clause in Entity Framework?

I have the following query

select distinct X.* from X
inner join Y on X.ID = Y.ID1
inner join Z on Y.ID= Z.ID2
where X.param = 1 and Z.param in (1,2)

So far I have the joins and the where clause on the X.param implemented but I'm struggling on the in clause of Z.param. The list of id's is supplied by a listofids of type List

entities.X
.Join(entities.Y, t1 => t1.ID, t2 => t2.ID1, (t1, t2) => new { X= t1, Y= t2 })
.Join(entities.Z, t => t.Y.ID, t3 => t3.ID2, (t, t3) => new { X= t.X, Y= t.Y, Z= t3 })
.Select(d => d.X).Distinct().Where(x1 => x1.param == 1)

Adding the following does not work

Any(d1 => d1.Z.param.Contains(listofids))

Any ideas?

Upvotes: 0

Views: 167

Answers (1)

Kristof Claes
Kristof Claes

Reputation: 10941

You need to reverse the Contains:

Any(d1 => listofids.Contains(d1.Z.param))

Upvotes: 2

Related Questions