How to perform LINQ query over Enum?

前端 未结 6 2006
甜味超标
甜味超标 2021-02-03 19:44

Below is my Enumerator List:

public enum StatusEnum
{
    Open = 1,
    Rejected = 2,
    Accepted = 3,
    Started = 4,
    Completed = 5,
    Canc         


        
6条回答
  •  生来不讨喜
    2021-02-03 20:13

    Steps:

    • Get the enum values and cast the results to the type of the enum
    • Sort the enum values by their integer values (otherwise they sort naturally by unsigned magnitude)
    • Take the first 4

    Code:

    return Enum.GetValues(typeof(Activity.StatusEnum))
    .Cast()
    .OrderBy(se =>(int)se)
    .Take(4);
    

    Output:

    Open Rejected Accepted Started

提交回复
热议问题