如何使用lambda表达式连接3个表?

inq*_*one 12 c# linq lambda join

我有一个简单的LINQ lambda连接查询,但我想添加一个带有where子句的第3个连接.我该怎么做呢?

这是我的单个连接查询:

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
    });
Run Code Online (Sandbox Code Playgroud)

我想将以下SQL命令添加到上面的LINQ查询中并仍然保持投影:

SELECT
    sector_code, industry_code 
FROM
    distribution_sector_industry 
WHERE
    service = 'numerical'
Run Code Online (Sandbox Code Playgroud)

第3次连接将使用Sector_code上的Sector表和Distribution_sector_industry进行.

提前致谢.

Dou*_*las 35

只是一个猜测:

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
});
Run Code Online (Sandbox Code Playgroud)


Jon*_*eet 15

好的,我不明白你为什么要在你已经知道的时候选择sector_code,但我想你想要这个:

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
            };
Run Code Online (Sandbox Code Playgroud)

笔记:

  • 因为这是一个我已经改变它变成一个查询表达式表达这样的查询的更易读的方式.
  • 虽然"where"子句在连接之后出现,但假设这是一个LINQ to SQL或Entity Framework查询,它应该没有任何区别
  • 为了清楚起见,我已经延长了范围变量名称
  • 我已将您的其他名称转换为传统的.NET名称; 你也可以在你的模型中做到这一点

  • 谢谢@JonSkeet。我承认您的方式更加简洁易读,但是我正在学习lambda,我更喜欢这种格式。谢谢。 (3认同)
  • @inquisitive_one:当你承认替代方案更具可读性时,你为什么喜欢某些东西?绝对值得知道这两种形式 - 当一个`Where`调用可以正常运行时,没有什么比查询表达式更简洁 - 但你应该使用在上下文中更易读的任何形式,IMO.当你有多个连接时,查询表达式几乎总是赢. (3认同)
  • +1:我同意这显然更具可读性. (2认同)

小智 5

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
     });
Run Code Online (Sandbox Code Playgroud)