So far I have come up with this:
list.Any(s => s.EnumIdValue == Close) && !list.Any(s => s.EnumIdValue == Reset || s.EnumIdValue == Cancel);
EnumIdValue contains several different possible values like Close, Reset, Cancel, SomeOtherState, etc. There should never be duplicates but it's still possible.
This does exactly what I want. But would there be a better (shorter) way to write this query?
Your original is fine. The other variant that would work is this:
var newQuery =
list.Any(s => s.EnumIdValue == EnumIdValue.Close) &&
list.All(s => s.EnumIdValue != EnumIdValue.Reset &&
s.EnumIdValue != EnumIdValue.Cancel);
In English: does the list have at least one Close
and is every item in the list not Reset
and not Cancel
?
By the way, sometimes formatting your code nicely makes a big difference in terms of readability.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With