2

userid我有这个查询,它返回我在access表中的用户的名字,我想要的是把那些不在表中的用户users还给我:useridaccess

 var query = db.Users
      .Join(db.Access, c => c.UserId, o => o.UserId,
       (c, o) => new { c.UserId, c.Name });

在sql中它会是这样的:

SELECT  Users.Name
FROM     Access INNER JOIN
               Users ON Access.UserId <> Users.UserId

什么lambda expression相当于sql脚本?

4

3 回答 3

6

采用Except

var res = db.Access.Except(query);
于 2013-03-12T12:50:55.330 回答
0

你也可以! Contains()像下面这样使用:

//Get Access user ids into an array
var AccessUserIds = db.Access.Select(a => new {a.UserId}).ToArray();

//Users who are not in Access user id array
var Results = db.Users.Where(u => !AccessUserIds.Contains(u.UserId));
于 2013-03-12T13:01:28.720 回答
0
var exclude = db.Access.Select(p => p.UserId);


var us = db.Users.Where(q => !exclude.Contains(q.UserId)).ToList();


ViewBag.UsersId = new SelectList(us, "UserId", "Name");

最后这是正确的答案,唯一的区别在于第一行。

谢谢你们!

于 2013-03-13T08:30:47.917 回答