将此SQL语句转换为LINQ-to-SQL

时间:2010-05-26 23:40:25

标签: linq-to-sql

我一直在努力将此SQL语句转换为LINQ to SQL VB.Net 9.0。我使用过Linqer但没有成功。任何帮助将不胜感激

select t.TeeId,
   t.DescriptionId,
   t.[Description],
   t.Rating,
   t.Slope,
   case when d.TotalHoles <> h.TotalHoles then 0
   else 1 end [Status]
from dbo.CourseDescription d
inner join dbo.CourseTees t
on t.DescriptionId = d.DescriptionId
inner join (select TeeId, count(*) as TotalHoles
           from dbo.CourseHoles
          group by TeeId) h
on h.TeeId = t.TeeId
where d.CourseId = 1

1 个答案:

答案 0 :(得分:0)

这是一个很好的选择。我没有在VB中进行任何编程,但我试图尽可能地使语法正确。为简单起见,我将其拆分为两个查询,但是使用LINQ to SQL,第一个实际上并不会导致查询数据库。它只是与第二个结合。在枚举第二个查询之前,两者都不会执行。如果需要,添加行继续。我不知道三元运算符是否存在SQL转换(存在于C#中)。如果没有,那么选择之前的部分,获取d.TotalHoles和h.TotalHoles,然后使用LINQ对象来枚举这些并构造状态。

dim holes = from h in db.TotalHoles
            groupby h.TeeId into g
            select TeeId = Key, TotalHoles = g.Count()

dim courses = from d in db.CourseDescription
              where d.CourseId = 1
              join t in CourseTees on d.DescriptionId equals t.DescriptionId
              join h in holes on h.TeeId equals t.TeeId
              select t.TeeId,
                     t.DescriptionId,
                     t.Description,
                     t.Rating, t.Slope,
                     Status  = If(d.TotalHoles = h.TotalHoles, 1, 0)