Linq to Sql IsNull 用现有 FK 替换 NULL FK
我需要将 sql 查询转换为 Linq to SQL 查询,但无法得到我期望的结果: 我有 2 个表,Base,M 表
BaseOne
{
MID (FK) , points to M
MChildID(FK, Nullable), points to M
}
M {
ID(PK)
}
简化的选择:
SELECT * from BaseOne as f
LEFT JOIN M as m ON ISNULL(f.MChildID, f.MID) = m.ID
我尝试了这个:
from f in BaseOne
from m in M.Where(function(x) If(f.MChildID.HasValue,x.ID.Equals(f.MChildID.Value),x.ID.Equals(f.MID.Value))).DefaultIfEmpty
它生成了这个 SQL:
...
(CASE
WHEN [t0].[MID] IS NOT NULL THEN
(CASE
WHEN [t1].[ID] = ([t0].[MID]) THEN 1
WHEN NOT ([t1].[ID] = ([t0].[MID])) THEN 0
ELSE NULL
END)
ELSE
(CASE
WHEN [t1].[ID] = ([t0].[MChildID]) THEN 1
WHEN NOT ([t1].[ID] = ([t0].[MChildID])) THEN 0
ELSE NULL
END)
END)) = 1
LEFT OUTER JOIN ...
我遵循 这些说明,但这个示例并不完全是我尝试做的。
有什么建议吗?
I need to transform an sql query to a Linq to SQL query, and i could'nt get the result what i expect:
I have 2 tables, Base, M tables
BaseOne
{
MID (FK) , points to M
MChildID(FK, Nullable), points to M
}
M {
ID(PK)
}
The Simplified select:
SELECT * from BaseOne as f
LEFT JOIN M as m ON ISNULL(f.MChildID, f.MID) = m.ID
I tried this:
from f in BaseOne
from m in M.Where(function(x) If(f.MChildID.HasValue,x.ID.Equals(f.MChildID.Value),x.ID.Equals(f.MID.Value))).DefaultIfEmpty
It generated this SQL:
...
(CASE
WHEN [t0].[MID] IS NOT NULL THEN
(CASE
WHEN [t1].[ID] = ([t0].[MID]) THEN 1
WHEN NOT ([t1].[ID] = ([t0].[MID])) THEN 0
ELSE NULL
END)
ELSE
(CASE
WHEN [t1].[ID] = ([t0].[MChildID]) THEN 1
WHEN NOT ([t1].[ID] = ([t0].[MChildID])) THEN 0
ELSE NULL
END)
END)) = 1
LEFT OUTER JOIN ...
I followed these instructions but this example is not exaclty what i try to do.
Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你尝试过VB的二进制if()运算符?
它产生一个COALESCE,类似于ISNULL。
Have you tried VB's binary if() operator?
It produces a COALESCE, which is similar to ISNULL.