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):
- 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}
- 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}
- 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.
.Containsexpressions intoWHERE x IN ( a, b, c )-type SQL predicate (which is identical toWHERE ( 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 anINNER JOINagainst a TVP.x.MyValueis anint, I believe you can try something like.Where(x => myEnums.Select(e => (int)e).Contains(x.MyValue)). The select converts theIEnumerable<MyEnums>to anIEnumerable<int>and the resulting SQL should be the equivalent ofWHERE x.MyValue IN (1, 2, 3). If that doesn't translate directly, you might need to map the enum values separately withList<int> MyEnumInts = myEnums.Select(e => (int)e).toList();and then use.Where(x => myEnumInts.Contains(x.MyValue)).