Linq与小组一起加入

时间:2015-04-04 07:17:52

标签: sql linq join

如何在linq中编写以下sql join?

select Campaign.CampaignName, COUNT(*) as total
 from Campaign join CampaignAsset
 on CampaignAsset.CampaignId=Campaign.CampaignId 
where Campaign.UserProfileId=65 
 Group By Campaign.CampaignName

1 个答案:

答案 0 :(得分:0)

试试这个:

        YourDatabaseName dataContext = new YourDatabaseName();

var result =  from c in dataContext.Campaign
              join ca in dataContext.CampaignAsset on c.CampaignId equals ca.CampaignId into j1
              from j2 in j1.DefaultIfEmpty()
              where c.UserProfileId = 65                  
              group j2 by c.CampaignName into grouped
              select new { CampaignName = grouped.Key, Count = grouped.Count() };