2

我的任务是将一些旧的 SQL 存储过程转换为 LINQ 以进行我们正在进行的 EF 迁移,我有点难过。鉴于这是从现有应用程序的迁移,edmx 是首先生成数据库的。所以,我有一个我试图复制的 SQL 语句:

SELECT DISTINCT
    d.DeliverySubscriptionId
,   d.ContactId
,   d.statementMacroId_fk
,   m.Name as MacroName
,   DeliveryMethod  =     REPLACE((SELECT DISTINCT
                                    dm2.Name + ',' AS 'data()'
                              FROM
                                    DeliverySubscription d2     
                              JOIN 
                                    StatementMacro m2 on 
                                    d2.StatementMacroId_fk = m2.StatementMacroId    
                              JOIN 
                                    DeliverySubscription_Method_Rel dmr2 ON 
                                    d2.DeliverySubscriptionId = dmr2.DeliverySubscriptionId_Fk
                              JOIN 
                                    dbo.DeliveryMethod dm2 ON 
                                    dmr2.DeliveryMethodId_Fk = dm2.DeliveryMethodId
                              WHERE
                                    d.DeliveryConfigurationId_fk = @configurationId
                                    AND 
                                    d.IsActive = 1
                                    AND
                                    D.DeliverySubscriptionId = D2.DeliverySubscriptionId FOR XML PATH('')) + '$', ',$', '') 

FROM 
      DeliverySubscription d     
INNER JOIN 
      StatementMacro m ON 
      d.StatementMacroId_fk = m.StatementMacroId    
JOIN 
      DeliverySubscription_Method_Rel dmr ON 
      d.DeliverySubscriptionId = dmr.DeliverySubscriptionId_Fk
JOIN dbo.DeliveryMethod dm ON 
      dmr.DeliveryMethodId_Fk = dm.DeliveryMethodId
WHERE 
      d.DeliveryConfigurationId_fk = @configurationId
      AND 
      d.IsActive = 1

特别是我遇到问题的部分是 JOIN ,它是一个关系表,表示和DeliverySubscription_Method_Rel之间的多对多关系。DeliverySubscriptionDeliveryMethod

这显示为:

[DeliverySubscription] * ---- * [Delivery Method]

在 edmx 的数据库图中。没有DeliverySubscription_Method_Rel创建实体。正如您在 SQL 语句中看到的,JOIN 直接在关系表上,但我似乎无法弄清楚如何在 LINQ 中复制它。请帮忙!

更新:所以环顾网络,我发现了一个类似的例子,它建议做这样的事情:

from s in Context.DeliverySubscriptions
                from dm in s.DeliveryMethods
                join sm in Context.StatementMacroes on s.StatementMacroId_Fk equals sm.StatementMacroId
                where s.DeliveryConfigurationId_Fk == configurationId
                select new DeliverySubscription_dto
                {
                    DeliverySubscriptionId = s.DeliverySubscriptionId,
                    QubeContactId = s.QubeContactId,
                    statementMacroId_fk = s.StatementMacroId_Fk,
                    MacroName = sm.Name,
                    DeliveryMethod = dm.Name.Replace("$","").Replace(",$","")
                }

...但是,因为到目前为止我在应用程序中还有很多其他的事情要更改,所以我无法构建以进行测试,所以我只是想运行这个,让大家看看这是否正确。

4

1 回答 1

0

该查询是一个野兽。如果您对它没有任何性能问题,我可能只是将它添加到数据模型中并使用 linq/EF 来调用它。

于 2013-12-27T21:32:27.990 回答