Compare the int values from enum list

72 views Asked by At

Having this enum :

public enum MyEnums
{
    A = 1,
    B = 2,
    C = 3,
    D = 4,
    E = 5
}

If I create a list with some enum values from MyEnums for example this :

IEnumerable<MyEnums> myEnums = new(){ MyEnums.A, MyEnums.B, MyEnums.C };

How can I check that a int value is in this list?

query.Where(x => myEnums.Contains((int)x.MyValue));

The problem is that the name of 'x.MyValue' is not the same then the name in 'MyEnums' only the int value is the same.

1

There are 1 answers

0
Bjorg P On

If the collection of values has a property that is an int value, then you can use the fact that an enum is actually an int under the hood. It isn't clear exactly what you want, so here are a few possibilities (and I create a collection of simple objects to show):

  1. You want to get a collection of the int values that are defined by the enum:
public enum MyEnum {A = 1, B = 2, C = 3, D = 4, E = 5}

public class ExampleType {
    public int MyValue { get; set;} 
}

public static void Main()
{
    IEnumerable<MyEnum> myEnums = new List<MyEnum>(){ MyEnum.A, MyEnum.B, MyEnum.C };
    IEnumerable<ExampleType> query = new List<ExampleType>(){ new ExampleType(){MyValue = 1}, new ExampleType(){MyValue = 2}, new ExampleType(){MyValue = 9}};
    var results = query.Where(x => Enum.IsDefined(typeof(MyEnum), x.MyValue)).Select(x => x.MyValue);
    foreach (var r in results) {
        Console.WriteLine(r);   
    }
}

The result is {1, 2}

  1. You want to get a collection of the enum values that are defined by the enum that happen to be in the collection of int values:
public enum MyEnum {A = 1, B = 2, C = 3, D = 4, E = 5}

public class ExampleType {
    public int MyValue { get; set;} 
}

public static void Main()
{
    IEnumerable<MyEnum> myEnums = new List<MyEnum>(){ MyEnum.A, MyEnum.B, MyEnum.C };
    IEnumerable<ExampleType> query = new List<ExampleType>(){ new ExampleType(){MyValue = 1}, new ExampleType(){MyValue = 2}, new ExampleType(){MyValue = 9}};
    var results = query.Where(x => Enum.IsDefined(typeof(MyEnum), x.MyValue)).Select(x => (MyEnum)x.MyValue);
    foreach (var r in results) {
        Console.WriteLine(r);   
    }
}

The result is {A, B}

  1. You want to get a collection of ExampleType objects that have an int value for a property that is defined by the enum:
public enum MyEnum {A = 1, B = 2, C = 3, D = 4, E = 5}

public class ExampleType {
    public int MyValue { get; set;} 
    
    public override string ToString() {
        return this.MyValue.ToString();
    }
}

public static void Main()
{
    IEnumerable<MyEnum> myEnums = new List<MyEnum>(){ MyEnum.A, MyEnum.B, MyEnum.C };
    IEnumerable<ExampleType> query = new List<ExampleType>(){ new ExampleType(){MyValue = 1}, new ExampleType(){MyValue = 2}, new ExampleType(){MyValue = 9}};
    var results = query.Where(x => Enum.IsDefined(typeof(MyEnum), x.MyValue));
    foreach (var r in results) {
        Console.WriteLine(r);   
    }
}

The result is two ExampleType objects that have MyValue property values that are valid values defined by MyEnum.