Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ to SQL where in (lambda syntax)

Tags:

c#

lambda

linq

Can someone help me with this please? I would like a simple 'where in'. Here's the SQL that does what I want.

select ur.RoleID
from UserRoles ur
where ur.RoleID in (5, 15)

And here's my attempt. The method .IN() doesn't exist obviously, just put my aggrevated thoughts lol.

int roleid;
foreach (data r in dataList) {
    using (DataContext communityContext = new DataContext()) {
        roleid = communityContext.UserRoles
            .Where(x => x.UserID == r.ClientId && x.RoleID.IN(5, 15))
            .Select(x => x.RoleID)
            .First();
    }
}
like image 860
Since_2008 Avatar asked Dec 22 '11 19:12

Since_2008


1 Answers

As you mention In doesn't exist, se .Contains() instead if you have a list, in your case you could also use x.RoleId == 5 || x.RoleId == 15

e.g.

var allowedRoles = new int[] { 5, 15 };

then in your where clause do:

allowedRoles.Contains(x.RoleID)
like image 103
Sebastian Piu Avatar answered Oct 04 '22 15:10

Sebastian Piu