我的任务是将一些旧的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
特别是我遇到问题的部分是JOINDeliverySubscription_Method_Rel
,它是一个表示DeliverySubscription
和DeliveryMethod
之间的多对多关系的关系表。
这显示为:
[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(",$","")
}
然而,因为到目前为止,我在应用程序中还有很多其他事情需要更改,所以我还无法构建来测试它,所以我只想运行这个buy-you all,看看这是否正确。
那个查询是个野兽。如果你对它没有任何性能问题,我可能会把它添加到数据模型中,并使用linq/EF来调用它。