-1

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.

12
  • 1
    You seem to be using Linq - is this for Linq-to-Objects (in-memory), or Linq-to-Entities (Entity Framework / Entity Framework Core?) - or some other Linq provider? Commented Jan 23, 2024 at 16:25
  • Linq-to-Entities, 'x.MyValue' is coming from the database. using EF Core. Commented Jan 23, 2024 at 16:32
  • Did you see Linq Contains with array of enum? Does that work for you? Commented Jan 23, 2024 at 16:47
  • 1
    @dbc EF and EFC for SQL Server converted .Contains expressions into WHERE x IN ( a, b, c )-type SQL predicate (which is identical to WHERE ( x = a OR x = b OR x = c )), which works okay for most cases, but when the set varies it means an explosion of new query-execution-plans - so in EF Core 7 (or 8? I forget) they added support for changing the query to use an INNER JOIN against a TVP. Commented Jan 23, 2024 at 17:30
  • 1
    If x.MyValue is an int, I believe you can try something like .Where(x => myEnums.Select(e => (int)e).Contains(x.MyValue)). The select converts the IEnumerable<MyEnums> to an IEnumerable<int> and the resulting SQL should be the equivalent of WHERE x.MyValue IN (1, 2, 3). If that doesn't translate directly, you might need to map the enum values separately with List<int> MyEnumInts = myEnums.Select(e => (int)e).toList(); and then use .Where(x => myEnumInts.Contains(x.MyValue)). Commented Jan 23, 2024 at 18:17

1 Answer 1

0

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.