Asp.net mvc LINQ到实体:如何在LINQ中实现这个复杂的t-sql(多个连接,不同的,不存在)?
我有一个复杂的查询,我正试图在LINQ中复制到实体,但我还没有,这可能吗 t-sql查询如下所示:Asp.net mvc LINQ到实体:如何在LINQ中实现这个复杂的t-sql(多个连接,不同的,不存在)?,asp.net-mvc,tsql,linq-to-entities,Asp.net Mvc,Tsql,Linq To Entities,我有一个复杂的查询,我正试图在LINQ中复制到实体,但我还没有,这可能吗 t-sql查询如下所示: select distinct C.id, L.id from dp join L on L.fk = DP.id join M on ( M.l_code = L.l_code and M.dp_code = DP.dp_code ) join C on C.c_code = M.c_code where not exists ( select id from map where m
select distinct
C.id,
L.id
from
dp
join L on L.fk = DP.id
join M on ( M.l_code = L.l_code and M.dp_code = DP.dp_code )
join C on C.c_code = M.c_code
where not exists ( select id from map where map.cid = c.id and map.lid = l.id )
IQueryable foo = from dp in ctx.DP
from l in dl.L
join m in ctx.M on l.l_code equals m.m_code
// Q1: how can I add: AND m.dp_code = dp.dp_code
join c in ctx.C on m.c_code = c.c_code
// this works, but why can't I do it as an AND in the join?
where m.dp_code == dp.dp_code
select new
{
cid = c.id,
cid = c.id
}.Distinct();
表如下所示:
DP:
id (pk)
dp_code (varchar)
L:
id (pk)
fk (fk to DP.ID)
l_code (varchar)
M:
id (pk)
l_code (varchar, matches value in L.l_code)
dp_code (varchar, matches value in DP.dp_code)
c_code (varchar, matches the value in C.c_code)
C:
id (pk)
c_code (varchar)
MAP:
id (pk)
cid (fk to C.id)
lid (fk to L.id)
我的LINQ看起来像:
select distinct
C.id,
L.id
from
dp
join L on L.fk = DP.id
join M on ( M.l_code = L.l_code and M.dp_code = DP.dp_code )
join C on C.c_code = M.c_code
where not exists ( select id from map where map.cid = c.id and map.lid = l.id )
IQueryable foo = from dp in ctx.DP
from l in dl.L
join m in ctx.M on l.l_code equals m.m_code
// Q1: how can I add: AND m.dp_code = dp.dp_code
join c in ctx.C on m.c_code = c.c_code
// this works, but why can't I do it as an AND in the join?
where m.dp_code == dp.dp_code
select new
{
cid = c.id,
cid = c.id
}.Distinct();
因此,问题是:
Q1:为什么我不能在连接中执行两个条件?LINQ中的用户错误或限制
问题2:如何将“不存在”添加到查询中?我已经看过了,但看不到如何实现notexists子查询
你可以,但是。但是,如果必须的话,可以使用匿名类型:在新的{l:l.l_代码上,d:dp.code}等于新的{l:m_代码,d:m.dp_代码}
哪里!从地图中的m开始,选择m.Any。但与1一样,最好使用关联
你可以,但是。但是,如果必须的话,可以使用匿名类型:在新的{l:l.l_代码上,d:dp.code}等于新的{l:m_代码,d:m.dp_代码}
哪里!从地图中的m开始,选择m.Any。但与1一样,最好使用关联
M中的数据来自不同的系统,表经常被删除和重新创建,并且数据质量不允许在db级别上有明确的fk关系。但是2正是我想要的,并且可能为我节省了几个小时——非常感谢!M中的数据来自不同的系统,表经常被删除和重新创建,并且数据质量不允许在db级别上有明确的fk关系。但是2正是我想要的,并且可能为我节省了几个小时——非常感谢!