Linq to Sql IsNull 用现有 FK 替换 NULL FK

发布于 2024-11-02 05:21:55 字数 1222 浏览 0 评论 0原文

我需要将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

别再吹冷风 2024-11-09 05:21:55

你尝试过VB的二进制if()运算符

from baseOne in BaseOne _
join m in M on if(baseOne.MChildID,baseOne.MID) equals m.ID _
select baseOne,m

它产生一个COALESCE,类似于ISNULL。

FROM [BaseOne] AS [t0]
INNER JOIN [M] AS [t1] ON (COALESCE([t0].[MChildID],[t0].[MID])) = [t1].[ID]

Have you tried VB's binary if() operator?

from baseOne in BaseOne _
join m in M on if(baseOne.MChildID,baseOne.MID) equals m.ID _
select baseOne,m

It produces a COALESCE, which is similar to ISNULL.

FROM [BaseOne] AS [t0]
INNER JOIN [M] AS [t1] ON (COALESCE([t0].[MChildID],[t0].[MID])) = [t1].[ID]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文