[英]How to join 3 tables with linq
I am trying to join 3 tables in a query with Linq to get data from all 3 tables.我正在尝试使用 Linq 在查询中加入 3 个表以从所有 3 个表中获取数据。 Below is an image of the table schemes:下面是表格方案的图像:
The query should select: SewagePlantName, CompanyName and Duty查询应选择:SewagePlantName、CompanyName 和 Duty
In addition I need to restricts the SewagePlantId to a list of Ids that are given as:此外,我需要将 SewagePlantId 限制为以下 Id 列表:
var sewagePlantIds = UnitOfWork.GetAll<UserGroup>()
.Where(group => group.Id == webAppPrincipal.GroupId)
.SelectMany(group => group.SewagePlantId).Select(sewageplant => sewageplant.Id).ToList();
I have difficulties with the order of joining the 3 tables and where/how to restrict the SewagePlantId to the given list.我对加入 3 个表的顺序以及在何处/如何将 SewagePlantId 限制为给定列表有困难。
Can you try something similar to it please for joining part你能尝试类似的东西吗请加入
from d in Duty
join c in Company on d.CompanyId equals c.id
join s in SewagePlant on c.SewagePlantId equals s.id
select new
{
duty = s.Duty.Duty,
CatId = s.Company.CompanyName,
SewagePlantName=s.SewagePlant.SewagePlantName
// other assignments
};
var obj = from trns in context.tblPartyRegistrations
join st in context.tblSellingTrans
on trns.PartyRegId equals st.Fk_PartyRegId
join pt in context.tblPartyRemainings
on trns.PartyRegId equals pt.fk_PartyId
select new
{
trns.Name,
trns.PhoneNo,
trns.Address,
st.RecivedAmount,
st.Date,
st.CustomerType,
st.MilkRate,
st.Mltr,
st.Mkg,
st.NtAmnt,
st.RemaningAmount,
pt.Remainingammount
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.