简体   繁体   中英

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

I have the following LinQ expression which is working fine. However, I wonder if it is possible to write the same query without using Lambda Expressions and 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.

Not without lambda expressions - that's how linq does most of its heavy lifting.

You can use .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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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