我想知道是否有人知道如何在Entity Framework中表达它在纯sql中的含义:
SELECT Name,IsEmployee,IsQualityNetwork FROM Person ORDER BY CASE WHEN IsQualityNetwork = 1 or IsEmployee = 1 THEN 0 ELSE 1 END,Name
我尝试使用Linq Dynamic但是当执行此代码时:
var p = ctx.People .OrderBy("CASE WHEN IsQualityNetwork = 1 or IsEmployee = 1 THEN 0 ELSE 1 END") .OrderBy(e => e.Name);
我得到了例外:
{“类型’人’中没有属性或字段’CASE’”}
解决方法
var p = ctx.People.OrderBy(p => (p.IsQualityNetwork == 1 || p.IsEmployee == 1) ? 0 : 1) .ThenBy(p => p.Name);