0

How do I write this linq query:

       List<List<string>> listOfLists = new List<List<string>>();

       listOfLists.Add(new List<string>(){"Item1", "Item2"});

       listOfLists.Add(new List<string>() { "Item2", "Item2" });


       //Does listOfLists contain at least one list that has one or more items?
4

1 回答 1

3

It sounds like you're trying to find if any list has any items. Two ways of doing that:

  • As described, using Enumerable.Any at both levels (once with a predicate and once without):

    var any = listOfLists.Any(list => list.Any());
    
  • Just flatten it and see if there are any items at all, as if there is at least one item, it must belong to a list with at least one item:

    var any = listOfLists.SelectMany(list => list).Any();
    
于 2013-07-07T18:37:33.877 回答