I have a collection with objects in it, one of these objects attribute is a PublishRequestStatus, which is an enum value.
I'm recieving another list of all enum values that it needs to match, now I know how to search for 1 attribute matching a value:
model = model.Where(x => x.PublishRequestStatus == PublishRequestStatus.Denied);
But I'm having trouble matching all possible values from another collection. For example if I have a collection with PublishRequestStatus.Approved
and PublishRequestStatus.Denied
, how would I use LINQ to search for all objects with either of those enum values?
The only thing that I found what works is to make one list for each enum value in the 2nd collection in a loop, and at the end join all the lists together. However, I'm pretty sure that LINQ has a better way of doing what I'm trying to achieve.
Something like this:
model = model.Where(x => myEnumList.Contains(x.PublishRequestStatus));
Alternativly with .Any()
:
model = model.Where(x => myEnumList.Any(y => y == x.PublishRequestStatus));
EDIT: According to Yuvals answer you may also simply check if your current element x
has any of the flags:
model = model.Where(x => x.PublishRequestStatus.HasFlags(otherFlags));
This assumes your enum looks similar to this:
[Flags]
enum MyEnum { ... }
Which will allow bitwise |
.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments