我想根据下面的SQL编写LINQ查询。
基本上这个策略看起来真的很混乱 - 为什么从MerchantGroupMerchant开始并从'语句中做2'?
问题:是否有更简单的方法来编写此LINQ查询?
var listOfCampaignsMerchantIsInvolvedIn =
(from merchantgroupactivity in uow.MerchantGroupActivities
from merchantgroupmerchant in uow.MerchantGroupMerchants
where merchantgroupmerchant.MerchantU.Id == merchantUIDGuid
select new
{
merchantgroupactivity.ActivityU.CampaignU.Id
}).Distinct();
这是表结构:
和SQL:
SELECT DISTINCT Campaign.ID
FROM Campaign
INNER JOIN Activity
ON ( Campaign.CampaignUID = Activity.CampaignUID )
INNER JOIN MerchantGroupActivity
ON ( Activity.ActivityUID = MerchantGroupActivity.ActivityUID )
INNER JOIN MerchantGroup
ON ( MerchantGroup.MerchantGroupUID = MerchantGroupActivity.MerchantGroupUID )
INNER JOIN MerchantGroupMerchant
ON ( MerchantGroupMerchant.MerchantGroupUID = MerchantGroup.MerchantGroupUID )
INNER JOIN Merchant
ON ( Merchant.MerchantUID = MerchantGroupMerchant.MerchantUID )
WHERE Merchant.ID = 'M1'
答案 0 :(得分:0)
不,不是真的,即使您使用视图来部分或完全减少查询大小,您的执行计划最终仍会看起来相同(并执行同样快/慢)。如果你必须遍历5个连接,那么你必须遍历5个连接,唯一的办法是通过引入说商家和活动或商家和活动之间的链接来“缩短”模型。你可以通过在它们之间引入M2M表来实现这一点(以手动维护为代价),但我不推荐它,除非检索确实是一个问题。如果此查询太慢,则应检查所有连接FK字段是否存在索引。