user1805430
user1805430

Reputation: 107

How to check if a nested list contains a particular value C#

I got a nested list with numbers of lists inside. I wanna check if this nested list contains a particular string value and it does not matter which list is the value stored in .

                        if (!checkList.Any(s => s == "aaa"))
                        {
                          // do sth
                        }

the above is to check normal list but not nested list, can anyone give me the answer for nested list?

Upvotes: 0

Views: 3171

Answers (1)

Douglas
Douglas

Reputation: 54897

Use a nested Any:

if (!checkList.Any(innerList => innerList.Any(s => s == "aaa")))

Alternatively, you may use a SelectMany to flatten your list:

if (!checkList.SelectMany(innerList => innerList).Any(s => s == "aaa"))

Upvotes: 7

Related Questions