如何编写具有多个联接的linq

时间:2019-06-21 05:28:35

标签: sql linq

作为一个初学者,我想以linq的形式获取以下查询并提供详细的说明

//我的SQL

select COL.title as organizationtitle,CL.[title] as 
cousestitle,sum(FD.feathers) as totalfeathers,sum(FD.amount) as 
totalamount
from  [dbo].[FeathersDonated]  FD 
join [dbo].[Couses] C  on FD.corpid=3 and FD.[cousesid]=C.id 
join [dbo].[Couses_lang] CL on FD.[cousesid]=CL.cousesid and 
CL.language='en-US'
JOIN [dbo].[Organization_lang] COL on COL.orgid=2 and COL.language='en 
US' 
group by FD.cousesid,CL.[title],CL.[description],COL.title

我尝试了以下代码集。请帮忙

var featherDonated = _GoUoW.FeathersDonated.FindBy(x => x.corpid == 
param.corpid)
.GroupBy(x => x.cousesid).Select(x => new { cousesid = x.Key, amount = 
x.Select(a => a.amount).DefaultIfEmpty(0).Sum(), feathers = x.Select(a => 
a.feathers).DefaultIfEmpty(0).Sum() })
.Join(_GoUoW.Couses.GetAll(), feather => feather.cousesid, couse => 
couse.id, (feather, couse) => new { feather, couse })
.Join(_GoUoW.Organization_lang.FindBy(orglang => orglang.language == "en- 
US"), couses => couses.couse.orgid, orgid => (param.organizationid > 0 ? 
param.organizationid : orgid.orgid), (couses, orgid) => new { couses, 
orgid 
})
.Join(_GoUoW.Couses_lang.FindBy(couselang => couselang.language == "en- 
US"), 
organization => organization.orgid.orgid, couselang => couselang.cousesid, 
(organization, couselang) => new { organization, couselang })
.Select(x => new
        {
            x.organization.couses.feather.amount,
            x.organization.couses.feather.feathers,
            x.couselang.title
            //x.organization.orgid.title,


        }).ToList();

0 个答案:

没有答案