如何用lambda表达式连接3个表? [英] How to join 3 tables with lambda expression?

查看:490
本文介绍了如何用lambda表达式连接3个表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

这是我的单个联接查询:

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

我想在上面的LINQ查询中添加以下SQL命令,并且仍然保持预测:

I want to add the following SQL command to the above LINQ query and still maintain the projections:

SELECT
    sector_code, industry_code 
FROM
    distribution_sector_industry 
WHERE
    service = 'numerical'

第3个连接将使用扇区"表&部门代码上的Distribution_sector_industry.

The 3rd join would be made with Sector table & Distribution_sector_industry on sector_code.

谢谢.

推荐答案

只是一个猜测:

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

这篇关于如何用lambda表达式连接3个表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆