Sql 不在 LINQ 查询的子查询子句中的查询
Sql Query with not in clause of subquery to LINQ query
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);
大家好,我是 entity framework 的新手。
我正在尝试将此查询转换为 LINQ 查询,但无法理解如何使用 not in 子句编写子查询。
另外,join 和 Include 哪个更好?
可能是这样的:
var list = userConnection.Where(user => user.useridto == 3464).Select(user => user.userid_from).ToList();
var list2 = userConnection.Where(user => !list.Contains(user.userid_to));
可能可以改进,因为包含需要相当长的时间(也许将列表放入哈希集中?)
这应该有效:
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
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;
Ov 通过 LEFT JOIN
var query =
from uc in ctx.user_user_connection
from uc2 in ctx.user_user_connection
.Where(uc2 => uc2.userid_from == 3464 && uc.userid_to == uc2.userid_from)
.DefaultIfEmpty()
where uc.userid_from = 3464 && uc2 == null
select uc;
这个
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
你可以添加另一个 where
from a in user_user_connection
where a.userid_from == 3464
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 * from user_user_connection
where userid_from = 3464 and
userid_to not in
(select userid_from from
user_user_connection
where userid_to = 3464);
大家好,我是 entity framework 的新手。 我正在尝试将此查询转换为 LINQ 查询,但无法理解如何使用 not in 子句编写子查询。 另外,join 和 Include 哪个更好?
可能是这样的:
var list = userConnection.Where(user => user.useridto == 3464).Select(user => user.userid_from).ToList();
var list2 = userConnection.Where(user => !list.Contains(user.userid_to));
可能可以改进,因为包含需要相当长的时间(也许将列表放入哈希集中?)
这应该有效:
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
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;
Ov 通过 LEFT JOIN
var query =
from uc in ctx.user_user_connection
from uc2 in ctx.user_user_connection
.Where(uc2 => uc2.userid_from == 3464 && uc.userid_to == uc2.userid_from)
.DefaultIfEmpty()
where uc.userid_from = 3464 && uc2 == null
select uc;
这个
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
你可以添加另一个 where
from a in user_user_connection
where a.userid_from == 3464
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;