Why does this return zero results?
I have a List<List<string>>
and when I try to search with the List<string>
it returns no results.
Any ideas?
Thanks
List<List<string>> te开发者_运维百科st = new List<List<string>>();
List<string> ff = new List<string>();
ff.Add("1");
ff.Add("ABC 1");
test.Add(ff);
ff = new List<string>();
ff.Add("2");
ff.Add("ABC 2");
test.Add(ff);
var result = test.Where(x=>x.Contains("ABC"));
//result.Count(); is 0
Neither of your lists contains the element "ABC".
If you want to find the lists that have an element that contains "ABC" as a substring you can do this:
var result = test.Where(x => x.Any(y => y.Contains("ABC")));
Its because you are doing a list of a list and not going far enough down in your selection. Something like this should give you two results:
var result = test.Select(x => x.Where(y => y.Contains("ABC")));
none of your lists contain the string "ABC". It doesn't search the string when you use that contains function, it just matches the whole string. If you want to search for a partial string, then you have to use something like the following:
var result = test.Where(x => x.Where(y => y.Contains("ABC").Count() > 0));
精彩评论