user3356020
user3356020

Reputation: 119

How can I get set of Enums except one using Linq?

I have a set of enums

public enum SyncRequestTypeEnum
{
 ProjectLevel=1,
 DiffSync=2,
 FullSync=3
}

I want to display these enums in a drop down list except ProjectLevel. Can I get these details using linq? Can someone help on this?

Upvotes: 5

Views: 5728

Answers (2)

George Feakes
George Feakes

Reputation: 251

Found myself in a similar situation where I needed the names of the Enum instead of their values.

To do that you could use this:

var exceptThese = new List<string> { nameof(SyncRequestTypeEnum.ProjectLevel) };
var result = Enum.GetNames<SyncRequestTypeEnum>().ToList().Except(exceptThese);

Upvotes: 0

Arion
Arion

Reputation: 31239

Maybe something like this:

var result = Enum
        .GetValues(typeof(SyncRequestTypeEnum))
        .Cast<SyncRequestTypeEnum>()
        .Where(w =>w!=SyncRequestTypeEnum.ProjectLevel)
        .ToList();

Upvotes: 12

Related Questions