LINQ to Entities中的SQL查询

时间:2011-10-05 13:58:18

标签: sql linq

有谁能告诉我,如何在LINQ中编写这个查询?

select a.UTP_NAME, b.UPS_NAME, c.USS_NAME from 
    TB_UTILIDADE_PUBLIC_UTP a inner join
    TB_UTILIDADE_PUBLIC_SECTOR_UPS b on 
    a.UPS_ID = b.UPS_ID 
       inner join TB_UTILIDADE_PUBLIC_SUBSECTOR_USS c 
 on a.USS_ID = c.USS_ID and a.UPS_ID = c.UPS_ID /* IMPORTANT LINE! */

感谢。

1 个答案:

答案 0 :(得分:2)

创建新的匿名类型允许您加入多个条件

var query = from a in context.TB_UTILIDADE_PUBLIC_UTP
            join b in context.TB_UTILIDADE_PUBLIC_SECTOR_UPS 
              on a.UPS_ID equals b.UPS_ID
            join c in context.TB_UTILIDADE_PUBLIC_SUBSECTOR_USS 
              on new { a.USS_ID, a.UPS_ID } equals new { c.USS_ID, c.UPS_ID }
            select new
            {
              a.UTP_NAME, 
              b.UPS_NAME, 
              c.USS_NAME
            };