C# 如何使用 lambda 表达式连接 3 个表?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9120088/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to join 3 tables with lambda expression?
提问by inquisitive_one
I have a simple LINQ lambda join query but I want to add a 3rd join with a where clause. How do I go about doing that?
我有一个简单的 LINQ lambda 连接查询,但我想添加一个带有 where 子句的第三个连接。我该怎么做?
Here's my single join query:
这是我的单个连接查询:
var myList = Companies
.Join(
Sectors,
comp => comp.Sector_code,
sect => sect.Sector_code,
(comp, sect) => new {Company = comp, Sector = sect} )
.Select( c => new {
c.Company.Equity_cusip,
c.Company.Company_name,
c.Company.Primary_exchange,
c.Company.Sector_code,
c.Sector.Description
});
I want to add the following SQL command to the above LINQ query and still maintain the projections:
我想将以下 SQL 命令添加到上面的 LINQ 查询并仍然保持预测:
SELECT
sector_code, industry_code
FROM
distribution_sector_industry
WHERE
service = 'numerical'
The 3rd join would be made with Sector table & Distribution_sector_industry on sector_code.
第 3 次连接将使用 Sector 表和 Distribution_sector_industry 上的 Sector_code。
Thanks in advance.
提前致谢。
采纳答案by Douglas
Just a guess:
只是猜测:
var myList = Companies
.Join(
Sectors,
comp => comp.Sector_code,
sect => sect.Sector_code,
(comp, sect) => new { Company = comp, Sector = sect })
.Join(
DistributionSectorIndustry.Where(dsi => dsi.Service == "numerical"),
cs => cs.Sector.Sector_code,
dsi => dsi.Sector_code,
(cs, dsi) => new { cs.Company, cs.Sector, IndustryCode = dsi.Industry_code })
.Select(c => new {
c.Company.Equity_cusip,
c.Company.Company_name,
c.Company.Primary_exchange,
c.Company.Sector_code,
c.Sector.Description,
c.IndustryCode
});
回答by user959729
Try something like this...
尝试这样的事情......
var myList = ({from a in Companies
join b in Sectors on a.Sector_code equals b.Sector_code
join c in Distribution on b.distribution_code equals a.distribution_code
select new {...});
回答by Jon Skeet
Okay, I can't see why you'd want to select sector_code when you already know it, but I think you want this:
好的,我不明白你为什么要在你已经知道的时候选择扇区代码,但我认为你想要这个:
var query = from company in Companies
join sector in Sectors
on company.SectorCode equals sector.SectorCode
join industry in DistributionSectorIndustry
on sector.SectorCode equals industry.SectorCode
where industry.Service == "numerical"
select new {
company.EquityCusip,
company.CompanyName,
company.PrimaryExchange,
company.SectorCode,
sector.Description,
industry.IndustryCode
};
Notes:
笔记:
- I've changed it into a query expression as that's a muchmore readable way of expressing a query like this.
- Although the "where" clause comes after the join, assuming this is a LINQ to SQL or Entity Framework query, it shouldn't make any difference
- I've lengthened the range variable names for clarity
- I've converted your other names into conventional .NET names; you can do this too in your model
- 因为这是一个我已经改变它变成一个查询表达式多表达这样的查询的更易读的方式。
- 尽管“where”子句出现在连接之后,假设这是一个 LINQ to SQL 或实体框架查询,它应该没有任何区别
- 为了清楚起见,我延长了范围变量名称
- 我已将您的其他名称转换为传统的 .NET 名称;你也可以在你的模型中做到这一点
回答by Septwaant Software
For 4 Tables
4桌
var query = CurrencyDeposits
.Join(Customers, cd => cd.CustomerId, cus => cus.Id, (cd, cus)
=> new { CurrencyDeposit = cd, Customer = cus })
.Join(Currencies, x => x.CurrencyDeposit.CurrencyId, cr => cr.Id, (x, cr)
=> new { x.CurrencyDeposit, x.Customer, Currency = cr })
.Join(Banks, x => x.CurrencyDeposit.BankId, bn => bn.Id, (x, bn)
=> new { x.CurrencyDeposit, x.Customer, x.Currency, Bank = bn})
.Select(s => new {
s.CurrencyDeposit.Id,
s.Customer.NameSurname,
s.Currency.Code,
s.Bank.BankName,
s.CurrencyDeposit.RequesCode
});

