首页 > 解决方案 > 如何编写具有多个连接的 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();

标签: sqllinq

解决方案


推荐阅读