How to perform LINQ query over Enum?

前端 未结 6 2008
甜味超标
甜味超标 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 19:47

    First, if possible, I'd make your enum values powers of 2, so they could be OR'd together.

    public enum StatusEnum
    {
        Open = 1,
        Rejected = 2,
        Accepted = 4,
        Started = 8,
        Completed = 16,
        Cancelled = 32,
        Assigned = 64
    }
    

    Then you could do something like this:

    public static List<Activity.StatusEnum> StatusList()
    {
        var statusesToShow = Activity.StatusEnum.Open | Activity.StatusEnum.Rejected | Activity.StatusEnum.Accepted | Activity.StatusEnum.Started;
    
        return Enum
            .GetValues(typeof(Activity.StatusEnum))
            .Cast<Activity.StatusEnum>()
            .Where(x => (x & statusesToShow) == x)
            .ToList();
    }
    

    EDIT: In light of the fact that you can't change the enum values, I'd just recommend you use something like:

    public static List<Activity.StatusEnum> StatusList()
    {
        return new List<Activity.StatusEnum> {
            Activity.StatusEnum.Open, 
            Activity.StatusEnum.Rejected, 
            Activity.StatusEnum.Accepted, 
            Activity.StatusEnum.Started
        };
    }
    
    0 讨论(0)
  • 2021-02-03 19:49

    How about something along the lines of:

    .Where(x => x <= Activity.StatusEnum.Started)
    
    0 讨论(0)
  • 2021-02-03 19:58

    ". . . only show the first 4 statuses and ignore the rest."

    To get the first n elements of an IEnumerable<T>, use the Take method:

    return Enum.GetValues(typeof(Activity.StatusEnum))
        .Cast<Activity.StatusEnum>()
        .Take(4)
        .ToList();
    
    0 讨论(0)
  • 2021-02-03 19:59
    return Enum.GetValues(typeof(Activity.StatusEnum)).Cast<Activity.StatusEnum>().Where((n, x) => x < 4);
    

    If you want to be able to change the list of items, just add them into a List<Activity.StatusEnum> and use Contains:

    var listValid = new List<Activity.StatusEnum>() { Activity.StatusEnum.Open, Activity.StatusEnum.Rejected, Activity.StatusEnum.Accepted, Activity.StatusEnum.Started };
    return Enum.GetValues(typeof(Activity.StatusEnum)).Cast<Activity.StatusEnum>().Where(n => listValid.Contains(n));
    
    0 讨论(0)
  • 2021-02-03 20:04

    Well if you're going to hard code the items that should be in the list anyway, why not just do this:

    public static List<Activity.StatusEnum> StatusList()
    {
        return new List<Activity.StatusEnum>
        { 
            Activity.StatusEnum.Open, 
            Activity.StatusEnum.Rejected, 
            Activity.StatusEnum.Accepted, 
            Activity.StatusEnum.Started 
        };
    }
    

    You could also dispose of the List<T> and just return the array itself. As long as you know these are the items you want, then there's no need for Linq.

    0 讨论(0)
  • 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<Activity.StatusEnum>()
    .OrderBy(se =>(int)se)
    .Take(4);
    

    Output:

    Open Rejected Accepted Started

    0 讨论(0)
提交回复
热议问题