user166013 user166013 - 1 month ago 9
C# Question

find item inside list of list

I have a list of customers having another list within customers called series. My series can be

null
or contain data.
How can I find any item matching series value. I have tried the two mehods below but with no success.

results = customers.Where(d => d.Series.Any(s => s.Contains("item")));

results = customers.Where(d => d.Series.Count() != 0).Where(s => s.Series.Contains("item"));


Thanks!

Answer

I would go with:

results = customers.Where(d => d.Series!= null && d.Series.Any(s => s.Contains("item")));

in case you want an element in Series to actually contain the string "item" or

results = customers.Where(d => d.Series!= null && d.Series.Any(s => s.Equals("item")));

in case you want an element in Series to be a perfect match to item.