How to find all objects with an attribute that matches any of the values in another collection?

anthonytimmers

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.

HimBromBeere

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How to find and print all AWK matches in bash?

From Dev

How to find all possible regex matches in python?

From Dev

How to find all regular expression matches in the file

From Dev

How to find all possible regex matches

From Dev

How do use regex to find match multiple matches for one attribute?

From Dev

How to find element that matches xslt attribute value in for-each loop

From Dev

Find all objects in collection Java Mongodb

From Dev

Adding all the values of a column if another column matches

From Dev

How to find all documents where the value of one field matches that of another

From Dev

Find all positions of all matches of one vector of values in second vector

From Dev

Find 5 greatest values for an attribute of a collection in MongoDB

From Dev

How to find all objects with an attribute that matches any of the values in another collection?

From Dev

How to find all the matches in a table?

From Dev

How to read attribute values from an array of objects?

From Dev

Xpath Getting All Nodes that Have an Attribute that Matches Another Node

From Dev

Get All possible combinations of objects, where sum of values matches number

From Dev

find common class for all objects in a collection

From Dev

Find all matches in string with regex in any order with Javascript

From Dev

How to find all regular expression matches in the file

From Dev

check if collection of objects where each contain another collection of objects contain all values of List<string> via LINQ

From Dev

addClass on all matches attribute

From Dev

How do use regex to find match multiple matches for one attribute?

From Dev

How to find element that matches xslt attribute value in for-each loop

From Dev

How to detect whether "find" found any matches?

From Dev

How to find all the matches in a table?

From Dev

Xpath Getting All Nodes that Have an Attribute that Matches Another Node

From Dev

Excel 2016: how to find all superstring matches in a column of cells from another column?

From Dev

Python: any() to check if attribute in List of Objects matches a list

From Dev

Excel VBA find values in another column if it matches

Related Related

  1. 1

    How to find and print all AWK matches in bash?

  2. 2

    How to find all possible regex matches in python?

  3. 3

    How to find all regular expression matches in the file

  4. 4

    How to find all possible regex matches

  5. 5

    How do use regex to find match multiple matches for one attribute?

  6. 6

    How to find element that matches xslt attribute value in for-each loop

  7. 7

    Find all objects in collection Java Mongodb

  8. 8

    Adding all the values of a column if another column matches

  9. 9

    How to find all documents where the value of one field matches that of another

  10. 10

    Find all positions of all matches of one vector of values in second vector

  11. 11

    Find 5 greatest values for an attribute of a collection in MongoDB

  12. 12

    How to find all objects with an attribute that matches any of the values in another collection?

  13. 13

    How to find all the matches in a table?

  14. 14

    How to read attribute values from an array of objects?

  15. 15

    Xpath Getting All Nodes that Have an Attribute that Matches Another Node

  16. 16

    Get All possible combinations of objects, where sum of values matches number

  17. 17

    find common class for all objects in a collection

  18. 18

    Find all matches in string with regex in any order with Javascript

  19. 19

    How to find all regular expression matches in the file

  20. 20

    check if collection of objects where each contain another collection of objects contain all values of List<string> via LINQ

  21. 21

    addClass on all matches attribute

  22. 22

    How do use regex to find match multiple matches for one attribute?

  23. 23

    How to find element that matches xslt attribute value in for-each loop

  24. 24

    How to detect whether "find" found any matches?

  25. 25

    How to find all the matches in a table?

  26. 26

    Xpath Getting All Nodes that Have an Attribute that Matches Another Node

  27. 27

    Excel 2016: how to find all superstring matches in a column of cells from another column?

  28. 28

    Python: any() to check if attribute in List of Objects matches a list

  29. 29

    Excel VBA find values in another column if it matches

HotTag

Archive