简体   繁体   English

使用不带Lambda表达式的LinQ在List类型属性上定义条件

[英]using LinQ without Lambda expressions to define condition on a List type property

I have the following LinQ expression which is working fine. 我有下面的LinQ表达式,它工作正常。 However, I wonder if it is possible to write the same query without using Lambda Expressions and Contains . 但是,我想知道是否可以在不使用Lambda Expressions和Contains的情况下编写相同的查询。 What would be another option? 还有什么选择? Since Roles is a list type, I could not figure out another way. 由于“ 角色”是一种列表类型,因此我找不到其他方法。

var users = (from p in _db.People
             where p.Roles.Select(r => r.RoleId).Contains(roleid)
             select p).ToList();

UPDATE 1: I meant: Not using Lambda, Contains, Select, etc. but using only select, join, from, where, etc. 更新1:我的意思是:不使用Lambda,Contains,Select等,而是仅使用select,join,from,where等。

Not without lambda expressions - that's how linq does most of its heavy lifting. 并非没有lambda表达式-这就是linq完成大部分繁重工作的方式。

You can use .Any(...) 您可以使用.Any(...)

var users =
(
    from p in _db.People
    where p.Roles.Any(r => r.RoleId == roleid)
    select p
).ToList();

This should generate the join you want... 这应该生成您想要的联接...

var users =
(
   from p in _db.People
   from r in p.Roles
   where r.RoleId == roleid
   select p
).ToList();

Edit: If you do not have relationships defined please try this (not tested)... 编辑:如果您没有定义关系,请尝试此(未测试)...

var query = (from p in db.people
join r in db.roles on p.roleID equals r.roleID
where r.roleID = roleID
select p).ToList();

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

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