0

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?

1
  • could you not just cast to int and use a >= or <= operator? Commented May 15, 2019 at 5:29

4 Answers 4

4

Use an array. Also, you don't need to use Where since you can pass the delegate to FirstOrDefault.

var lookFor = new [] { MyEnum.A, MyEnum.B, MyEnum.C };
return list.FirstOrDefault(r => lookFor.Contains(r.MyEnumValue));
Sign up to request clarification or add additional context in comments.

1 Comment

why not !=, it will reduce linq query as well as easy to read
2

This is the shortest I can think of. Not sure if that makes the intent much clearer however.

list.Where(r => r.MyEnumValue == myEnum || (myEnum == MyEnum.B && r.MyEnumValue == MyEnum.A)).FirstOrDefault();

2 Comments

Hey @Botz, thanks, but in my taste it is still not clearer to maintain than the simple If else statement. See the updated question, I've also added C to compare with. I'm search for a design based solution.
why not !=, it will reduce linq query as well as easy to read
1

what about !=

 return list.Where(r =>
                        r.MyEnumValue != MyEnum.Undefined  || 
                        r.MyEnumValue != MyEnum.D
                    ).FirstOrDefault();

In real case we can take less elements in lookFor array and use negation, something like

var lookFor = new [] { MyEnum.Undefined, MyEnum.D};
return list.FirstOrDefault(r => !lookFor.Contains(r.MyEnumValue));

Please check negation in FirstOrDefault query, this will help us to reduce elements in loofFor array

2 Comments

Good answer, but for me working with != is less intuitive to understand but still upvoted.
Agreed.. happy coding
0

Ended up mixing @john-wu and @botz3000 answers with:

var lookFor = new [] { MyEnum.A, MyEnum.B, MyEnum.C };

list.FirstOrDefault(r => r.MyEnumValue == myEnum || (
                             myEnum == MyEnum.B && lookFor.Contains(r.MyEnumValue)
                         )
                    );

3 Comments

why not !=, it will reduce linq query as well as easy to read
Its true in the case of the simplified question, but in the real code there are many enum options
I updated my answer as you mentioned in above 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.