使用case将SQL组转换为Linq并按顺序排列

时间:2015-08-13 06:45:53

标签: c# sql linq

我在sql查询中有一个Group by和Order by,如下所示,如何在linq中编写相同的逻辑? : -

  GROUP BY bd.bandcharge_desc, bd.rate 
  ORDER BY CASE 
           WHEN bd.bandcharge_desc LIKE '%above%' THEN 
             '1' 
           ELSE 
             '0' 
           END,
           bd.bandcharge_desc,bd.rate ASC

2 个答案:

答案 0 :(得分:1)

试试这种方式

orderby bd.bandcharge_desc.Contains("above")?1:0 descending ,
               bd.bandcharge_desc,bd.rate

答案 1 :(得分:0)

试一试:

.OrderBy(bd => bd.bandcharge_desc.Contains("above") ? 1 : 0)
.ThenBy(bd => bd.bandcharge_desc)
.ThenBy(bd => bd.rate)
.GroupBy(bd => new {bd.bandcharge_desc, bd.rate})