Chandra
Chandra

Reputation: 92

How to Query 'In clause' upon collection of Collection in Linq

I have a list of Ids and I want to fire a 'In Clause' query on nested collection.How would i do that. In more detail I have 2 tables

Table : A Table : B

These both tables have their respective primary keys,they are using another "table C" to store their mapping details. Table : C Table C contains two columns both are the primary keys of respective table A and Table B. Now I have Ids from Table A, and get all the records from B which are associated with A using Table C. I want to fire an 'In Query' just like Sql in Linq but not be able to fire on collections of Table C.

I know in linq we have WhereIn clause as well. but its not showing in the list of it.

My query is something like that.

       Context.B.Where(item=>item.C.WhereIn(item1=>Item1.AID==aid));

I want something like that query but with 'C' its not showing the wherein clause as well what should i do any suggestion.

Upvotes: 1

Views: 143

Answers (1)

Christos
Christos

Reputation: 53958

Try this one:

// Select the ids of the table B that are associated with ids in aIds.
var bIds = Context.C.Where(x=>aIds.Contains(x.aId)).Select(x=>x.bId);

// Get the record of table B, which their ids are in bIds
var result = Context.B.Where(x=>bIds.Contains(x.Id));

Upvotes: 1

Related Questions