How can I check if any List<string>s in a List contain a given string? I know how to do this with a loop, but is there a way with LINQ/in one line?
if (lists.Any(sublist => sublist.Contains(str)))
var t=lists.SelectMany(f=>f).Contains("str");
full sample :
var lists = new List<List<string>>();
lists.Add(new List<string>(){"a","b"});
lists.Add(new List<string>(){"b","2"});
lists.Add(new List<string>(){"c","5"});
lists.Add(new List<string>(){"d","7"});
var t=lists.SelectMany(f=>f);
t.Dump();

if (t.Contains("k"))
Console.WriteLine ("yes") ;
else
Console.WriteLine ("no");
result
no
p.s.
ofcourse - this can be shorten to :
if (lists.SelectMany(f=>f).Contains("k"))...
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