问题描述
select * from user_user_connection
where userid_from = 3464 and
userid_to not in
(select userid_from from
user_user_connection
where userid_to = 3464);
大家好,我是实体框架的新手。 我正在尝试将此查询转换为 LINQ 查询,但无法理解如何使用 not in 子句编写子查询。 另外,加入或包含哪个更好?
解决方法
这个
from a in user_user_connection
join b in user_user_connection on
new {From=a.userid_to,To=a.userid_from} equals new {From=b.userid_from,To=b.userid_to} into c
from d in c.DefaultIfEmpty()
where d == null
select a;
类似于
select a.*
from user_user_connection a
left join user_user_connection b on a.userid_to = b.userid_from and a.userid_from = b.userid_to
where b.userid_from is null
这应该与您不在查询中的匹配。
如果您想要一个特定的 userid_from
,您可以添加另一个位置
from a in user_user_connection
where a.userid_from == 3464
join b in user_user_connection on
new {From=a.userid_to,To=b.userid_to} into c
from d in c.DefaultIfEmpty()
where d == null
select a;
,
可能是这样的:
var list = userConnection.Where(user => user.useridto == 3464).Select(user => user.userid_from).ToList();
var list2 = userConnection.Where(user => !list.Contains(user.userid_to));
可能可以改进,因为包含需要相当长的时间(也许将列表放在哈希集中?)
,这应该有效:
[routerLink]="['',{ outlets: { side-mod: ['table-list'] } }]"
或通过var query =
from uc in ctx.user_user_connection
where uc.userid_from = 3464 &&
!(from uc2 in ctx.user_user_connection
where uc2.userid_to = 3464
select uc2.userid_from).Contains(uc.userid_to)
select uc;
EXISTS
Ov 通过 var query =
from uc in ctx.user_user_connection
where uc.userid_from = 3464 &&
!(from uc2 in ctx.user_user_connection
where uc2.userid_to = 3464 && uc.userid_to == uc2.userid_from
select uc2).Any()
select uc;
LEFT JOIN