[英]lambda linq not join c#
I have this query that returns me the names of the users whose userid
is in access
table and users
, what I want is to give me back those userid
of users who are not in access
.: 我有这样的查询,返回其我的用户名userid
是在access
表users
,我要的是给我回那些userid
谁在没有用户的access
:
var query = db.Users
.Join(db.Access, c => c.UserId, o => o.UserId,
(c, o) => new { c.UserId, c.Name });
In sql it´d be something like this: 在SQL中将是这样的:
SELECT Users.Name
FROM Access INNER JOIN
Users ON Access.UserId <> Users.UserId
What is the lambda expression
equivalent to the sql script? 与SQL脚本等效的lambda expression
是什么?
使用Except
var res = db.Access.Except(query);
You can also use ! Contains()
您也可以使用! Contains()
! Contains()
like below: ! 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));
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");
Finally that is the right answer, the only difference is in the first line. 最后,这是正确的答案,唯一的不同是第一行。
Thank you all! 谢谢你们!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.