如何在此lambda表达式中指定“not in”?

时间:2012-01-13 13:16:59

标签: c# .net linq-to-sql lambda

我有一个简单的问题,因为我的大脑不适合我...... 我在哪里指定我希望“用户”中的用户ID不在“群组”中?

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
});

2 个答案:

答案 0 :(得分:11)

以下内容应该有效(假设我正确理解了您的问题):

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
    });

答案 1 :(得分:0)

你可以尝试这样的事情:

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
         };

看看这里:http://introducinglinq.com/blogs/marcorusso/archive/2008/01/14/the-not-in-clause-in-linq-to-sql.aspx