gpt4 book ai didi

c# - 如何使用 linq 连接 3 个表

转载 作者:行者123 更新时间:2023-12-01 19:46:35 24 4
gpt4 key购买 nike

我正在尝试使用 Linq 在查询中连接 3 个表,以从所有 3 个表中获取数据。下面是表格方案的图像:

enter image description here

查询应选择:SewagePlantName、CompanyName 和 Duty

此外,我需要将 SewagePlantId 限制为 Id 列表,如下所示:

            var sewagePlantIds = UnitOfWork.GetAll<UserGroup>()
.Where(group => group.Id == webAppPrincipal.GroupId)
.SelectMany(group => group.SewagePlantId).Select(sewageplant => sewageplant.Id).ToList();

我对连接 3 个表的顺序以及在何处/如何将 SewagePlantId 限制为给定列表感到困难。

最佳答案

您可以尝试类似的方法来加入部分

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
};

关于c# - 如何使用 linq 连接 3 个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41933985/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com