简体   繁体   English

通过枚举对LINQ表达式的结果进行排序

[英]Order results of LINQ expression by an Enum

I have the following model entity: 我有以下模型实体:

public class Itinerary 
{
    public int Id {get; set }
    public DateTime? Date { get; set; }
    public EnumType Type { get; set; }
}

public enum EnumType
{
    Start,
    Intermediary,
    End
}

I would like to sort first based on the Enum (property named Type ) then based on the DateTime (property named Date ). 我想首先基于Enum(名为Type属性)进行排序,然后基于DateTime(名为Date属性)进行排序。 I already tried the expression below but this is not ordered by Type first 我已经尝试过下面的表达式,但这不是按Type排序的

var itineraries = t.Itineraries.OrderBy(x => x.Type).OrderBy(x => x.Date).ToList();

How to order results of my LINQ expression, first by Type (order: Start, Intermediary, End) then by Date ? 如何首先按Type (顺序:开始,中间,结束)然后按Date对LINQ表达式的结果进行排序?

The reason is because sometimes there are no dates defined then I need to sort by type. 原因是因为有时没有定义日期,所以我需要按类型排序。

使用ThenBy进一步排序:

var itineraries = t.Itineraries.OrderBy(x => x.Type).ThenBy(x => x.Date).ToList();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM