LINQ
序列排序

序列排序

⚠️

该文档可能已过期。

  • OrderByCase / ThenByCase:按指定字符串序列排序。

例如,查询地区(Regions)表,将结果按 N / E / W / S 的地区序列排序返回:

RegionIDRegionDescription
1Eastern
2Western
3Northern
4Southern
var query = sqlite.Regions
    .OrderByCase(x => x.RegionDescription, new[]
    {
        "Northern",
        "Eastern",
        "Western",
        "Southern",
    });
var result = query.Select(x => new
{
    x.RegionID,
    x.RegionDescription,
});

执行 SQL:

SELECT *
FROM "Regions" AS "r"
ORDER BY CASE
    WHEN "r"."RegionDescription" = 'Northern' THEN 0
    ELSE CASE
        WHEN "r"."RegionDescription" = 'Eastern' THEN 1
        ELSE CASE
            WHEN "r"."RegionDescription" = 'Western' THEN 2
            ELSE CASE
                WHEN "r"."RegionDescription" = 'Southern' THEN 3
                ELSE 4
            END
        END
    END
END;

运行结果:

RegionIDRegionDescription
3Northern
1Eastern
2Western
4Southern