如何将此SQL Order By语句转换为LINQ-To-Object?

时间:2012-09-18 14:26:45

标签: c# sql linq linq-to-sql sql-order-by

这是SQL ORDER BY:

order by case when [order] is null then 1 else 0 end, [order]

如何在LINQ-To-Object语句中编写它?

2 个答案:

答案 0 :(得分:3)

方法语法

var query = enumerable.OrderBy(x => x.order == null ? 1 : 0).ThenBy(x => x.order);

查询语法

var query = from x in enumerable
            orderby (x.order == null ? 1 : 0), x.order
            select x;

答案 1 :(得分:0)

假设订单是可以为空的值(不是对象),您应该能够使用它:

orderby null == order ? 1 : 0, order

如果是对象,可以尝试

orderby null == order ? 1 : 0, order.PropertyName