How to join 3 tables with lambda expression?

后端 未结 4 2139
借酒劲吻你
借酒劲吻你 2021-01-31 11:47

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         


        
4条回答
  •  忘掉有多难
    2021-01-31 12:17

    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 much more 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

提交回复
热议问题