In order to simplify, let say I have MyEnum and MyClass:
enum MyEnum
{
Undefined = 0,
A = 1,
B = 2,
C = 3,
D = 4
}
class MyClass { MyEnum MyEnumValue { get; set; } }
In order to filter a list of MyClass by a MyEnum value I'm using:
list.Where(r => r.MyEnumValue == myEnum);
But if the MyEnum is equal to specific value (lets say MyEnum.B) I would need the list to return also any values that equal to MyEnum.B or MyEnum.A.
This is what I've came up with:
public static MyClass MyClass_By_MyEnum(IEnumarable<MyClass> list, MyEnum myEnum)
{
if (myEnum == MyEnum.B)
{
return list.Where(r =>
r.MyEnumValue == MyEnum.A ||
r.MyEnumValue == MyEnum.B ||
r.MyEnumValue == MyEnum.C
).FirstOrDefault();
}
else
{
return list.Where(r => r.MyEnumValue == myEnum).FirstOrDefault();
}
}
Is there any way achieving this using one line only? without the if else statement?
EDIT 1: I'm searching for a better design based solution, any suggestion?
>=or<=operator?