Reputation: 4076
how can I simulate the following sql query using linq
Select * From Resource Where Id in
(
Select ResourceId From ResourceRelCategory Where CategoryId =8
)
note that Resource and Category have Many to Many relationship and ResourceRelCategory is an association table between them. thanks
Upvotes: 0
Views: 360
Reputation: 23113
You could try something like:
var result = from r in Resource
where (
select c from ResourceRelCategory
where c.CategoryId==8
select c.ResourceId
).Contains(r.Id)
select r;
or
var result = from r in Resource
where r.Categories.Any(c => c.Id == 8)
select r;
or maybee the other way around:
Category.First(c => c.Id == 8).Resources
Upvotes: 2
Reputation: 496
See the article about this here I have already posted about the same at http://www.codeproject.com/Tips/336253/Filtering-records-from-List-based-similar-to-Sql-I
Upvotes: 0