繁体   English   中英

通过 Linq 在运算符中使用 sql

[英]using sql in operator by Linq

如何使用 linq 模拟以下 sql 查询

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

请注意,Resource 和 Category 具有多对多关系,ResourceRelCategory 是它们之间的关联表。 谢谢

您可以尝试以下方法:

var result = from r in Resource
             where (
                 select c from ResourceRelCategory
                 where c.CategoryId==8
                 select c.ResourceId
             ).Contains(r.Id)
             select r;

或者

var result = from r in Resource
             where r.Categories.Any(c => c.Id == 8)
             select r;

或者反过来:

Category.First(c => c.Id == 8).Resources

请参阅此处有关此的文章我已经在http://www.codeproject.com/Tips/336253/Filtering-records-from-List-based-similar-to-Sql-I上发布了相同的文章

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM