我必须修改一个查询以添加一个额外的连接来引入一个分区名称。
var c = from C in db.Campaigns
join L in db.Links on C.id equals L.campaignID into CL
from CL2 in CL.DefaultIfEmpty()
join D in db.Divisions on CL2.divisionID equals D.id into CLD
from CLD2 in CLD.DefaultIfEmpty()
where C.ClientID == user.ClientID
group CLD2 by new { C.id, C.Name, Division = CLD2.Name, C.created } into G
select new {
CampaignName = G.Key.Name,
Campaignid = G.Key.id,
Division = G.Key.Division ?? "-",
Created = G.Key.created,
Links = G.Count(b => b.Name != null) };
我遇到的问题是,当链接数为0时,每个行的分区名称为空(并且它不应该是)。
所以我尝试使用Lambda以不同的方式编写它:
var c =
from CLD in
(
from C in db.Campaigns
from L in db.Links.Where(w => w.campaignID == C.id).DefaultIfEmpty()
from D in db.Divisions.Where(w => w.id == C.DivisionID).DefaultIfEmpty()
where C.ClientID == user.ClientID
select new
{
CampaignName = C.Name,
Campaignid = C.id,
Division = D.Name,
Created = C.created
}
)
group CLD by new { id = CLD.Campaignid, Name = CLD.CampaignName, Division = CLD.Division, created = CLD.Created } into G
select new { CampaignName = G.Key.Name, Campaignid = G.Key.id, Division = G.Key.Division ?? "-", Created = G.Key.created, Links = G.Count(b => b.Campaignid != null) };
在此查询中,所有行的分区名称都正确显示,但当Links的数量为0时,查询返回1。
我确信我在两种情况下都很接近,但似乎无法找到正确的语法。欢迎任何建议。很高兴为任何一个查询修复,但最终会看到如何解决这两个问题。
谢谢大家。
答案 0 :(得分:0)
好吧,我有人看着它并帮我整理一下。这是一个更简洁的查询,更完整的lambda比我的第二个更混乱。所以希望这个例子可以帮助其他人。
{{1}}