如何在LINQ中进行子选择?

时间:2011-09-21 17:09:48

标签: c# sql-server linq

我是SQL服务器的新手,请任何机构帮我解决如何在LINQ中编写此查询。

 SELECT [Plan_Num]
      ,(select top 1 ba_level_code + ' - ' + ba_level_desc from baLevel where ba_level_code = '0' + Level_Num) as [Level]
      ,(select top 1 cast(Column_Num as varchar) + ' - ' + Column_Description from baPlanColumnStructure where Column_Num = CL.Column_Num) as [Column]
      ,[Sort_Order]
  FROM baCodeLibrary CL where code_id = 25468 and isactive = 1 order by [Plan_num]

由于

1 个答案:

答案 0 :(得分:7)

您正在寻找类似的东西

var query = from cl in context.BaCodeLibrary
            where cl.code_id == 25468 && cl.isactive == 1
            orderby cl.Plan_num
            select new
            {
              Level = (from ba in context.baLevel
                       where ba.ba_level_code == ("0" + ba.Level_Num)
                       select ba.ba_level_code + " - " + ba.ba_level_desc).Take(1),

              Column = (from ba in context.baPlanColumnStructure 
                        where ba.Column_Num == cl.Column_Num
                        select ba.ba_level_code + " - " + ba.ba_level_desc).Take(1),

               Sort_Order = cl.Sort_Order
            }