How to specify "not in" in this lambda expression?

I have a quick question, because my brain will not work with me ...
Where can I indicate that I want user_id in 'Users' not to be in 'Groups'?

db.Users.Join(db.Groups, a => a.user_id, b => b.user_id, (a, b) => new SelectListItem
{
  Value = a.user_id.ToString(),
  Text = a.surname + " " + a.lastname
});
+5
source share
2 answers

The following should work (assuming I understood your question correctly):

db.Users
.Where(x => !db.Groups.Any(y => y.user_id == x.user_id))
.Select(a => new SelectListItem
    {
        Value = a.user_id.ToString(),
        Text = a.surname + " " + a.lastname
    });
+11
source

you can try something like this:

var query =  from u in db.Users
             where !(from g in dc.Groups
                     select g.user_id)
                     .Contains(u.user_id)
             select new SelectListItem { 
                         Value = u.user_id.ToString(),
                     Text = u.surname + " " + u.lastname
         };

Take a look here: http://introducinglinq.com/blogs/marcorusso/archive/2008/01/14/the-not-in-clause-in-linq-to-sql.aspx

0
source

All Articles