user1977591
user1977591

Reputation: 211

How can I write this query in linq?

I know there are probably a ton of questions like this already but i'm having trouble

select * 
from [Group]
where GroupId not in 
(select GroupId
from CustomerGroup
where CustomerId = 189621)

i have myGroups = db.Groups.Where(e=> e.GroupId), but how do i say not in?

i am kind of there

var myGroups = from a in db.Groups
where!(from b in db.CustomerGroups
where b.CustomerId == customer.CustomerId )

Upvotes: 1

Views: 48

Answers (2)

hendryanw
hendryanw

Reputation: 1937

Using lambda expression, you probably can do it like this

var groupIds = db.CustomerGroups.Where(x => x.CustomerId == 189621).Select(x => x.GroupId);
var myGroups = db.CustomerGroups.Where(x => !groupIds.Contains(x.GroupId)).ToList();

Upvotes: 1

Brad Christie
Brad Christie

Reputation: 101614

var groupIds = from cg in db.CustomerGroups
               where cg.CustomerId == 189621
               select cg.GroupId;
var myGroups = from g in db.Groups
               where !groupIds.Contains(g.GroupId)
               select g;

Need a list to disqualify first. NOT IN is basically a !(IEnumerable).Contains() in LINQ (since you're comparing to a set).

Upvotes: 2

Related Questions