开发者

using sql in operator by Linq

开发者 https://www.devze.com 2023-03-20 13:52 出处:网络
how can I simulate the following sql query using linq Select * From Resource Where Id in ( Select ResourceId From ResourceRelCategory Where CategoryId =8

how can I simulate the following sql query using linq

Select * From Resource Where Id in
(
Select ResourceId From ResourceRelCategory Where CategoryId =8 
)

note th开发者_运维知识库at Resource and Category have Many to Many relationship and ResourceRelCategory is an association table between them. thanks


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消