entity-framework – 实体框架OrderBy“CASE WHEN”

前端之家收集整理的这篇文章主要介绍了entity-framework – 实体框架OrderBy“CASE WHEN”前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想知道是否有人知道如何在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);
原文链接:https://www.f2er.com/mssql/78843.html

猜你在找的MsSQL相关文章