如何使用linq联接3个表 [英] How to join 3 tables with linq
本文介绍了如何使用linq联接3个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用Linq在查询中联接3个表以从所有3个表中获取数据.下面是表方案的图像:
I am trying to join 3 tables in a query with Linq to get data from all 3 tables. Below is an image of the table schemes:
查询应选择:SewagePlantName,CompanyName和Duty
The query should select: SewagePlantName, CompanyName and Duty
此外,我需要将SewagePlantId限制为以以下形式给出的ID列表:
In addition I need to restricts the SewagePlantId to a list of Ids that are given as:
var sewagePlantIds = UnitOfWork.GetAll<UserGroup>()
.Where(group => group.Id == webAppPrincipal.GroupId)
.SelectMany(group => group.SewagePlantId).Select(sewageplant => sewageplant.Id).ToList();
在连接3个表的顺序以及将SewagePlantId限制在给定列表的位置/方式方面,我遇到了困难.
I have difficulties with the order of joining the 3 tables and where/how to restrict the SewagePlantId to the given list.
推荐答案
可以加入类似的东西吗
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
};
这篇关于如何使用linq联接3个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文