Reputation: 8291
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.
Upvotes: 0
Views: 2894
Reputation:
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();
Upvotes: 1
Reputation: 117144
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();
Upvotes: 1