Reputation: 40032
At the moment I have some code like below which based on a NoOfRows properties returns whether all data has been entered into list(s):
switch (NoOfRows)
{
case 1:
return InputList1.Any();
case 2:
return InputList1.Any() && InputList2.Any();
case 3:
return InputList1.Any() && InputList2.Any() && InputList3.Any();
case 4:
return InputList1.Any() && InputList2.Any() && InputList3.Any() && InputList4.Any();
case 5:
return InputList1.Any() && InputList2.Any() && InputList3.Any() && InputList4.Any() && InputList5.Any();
case 6:
return InputList1.Any() && InputList2.Any() && InputList3.Any() && InputList4.Any() && InputList5.Any() && InputList6.Any();
case 7:
return InputList1.Any() && InputList2.Any() && InputList3.Any() && InputList4.Any() && InputList5.Any() && InputList6.Any() && InputList7.Any();
case 8:
return InputList1.Any() && InputList2.Any() && InputList3.Any() && InputList4.Any() && InputList5.Any() && InputList6.Any() && InputList7.Any() && InputList8.Any();
case 9:
return InputList1.Any() && InputList2.Any() && InputList3.Any() && InputList4.Any() && InputList5.Any() && InputList6.Any() && InputList7.Any() && InputList8.Any() && InputList9.Any();
case 10:
return InputList1.Any() && InputList2.Any() && InputList3.Any() && InputList4.Any() && InputList5.Any() && InputList6.Any() && InputList7.Any() && InputList8.Any() && InputList9.Any() && InputList10.Any();
default:
return false;
}
I'm thinking it might be better to refactor this code and have an List<List<int>> or a Dictionary<int,List<int>>
but how would I do the above to return whether each list in the collection has something in it?
List<List<int>> values = new List<List<int>>(){InputList1, InputList2 ... InputList10};
var lists = values.Take(NoOfRows);
lists.. //check each item and return value of Any for each one
Upvotes: 2
Views: 179
Reputation: 38397
If you just want to know if any sub-list is empty, try this:
var values = new List<List<int>>(); // create and load up...
...
// returns true if any sub-list is EMPTY
var isAnyEmpty = values.Any(l => !l.Any());
This will check if there exists any list in the list of lists that does not have an item.
However, if you want the indexes of those sub-lists that are empty, you can just store the results of the Any() on all:
// will return a lists of bools for Any() applied to each sub-list
var listStates = values.Select(l => l.Any()).ToList();
Then, the index of any result where the value is false
will be a list without any items. Or, alternatively, you could use the special form of Select
that pass along indexes directly.
Upvotes: 6
Reputation: 2488
Do you mean something like this:
List<List<int>> list = new List<List<int>>();
bool anyNonEmpty = list.Any(x => x.Any());
Upvotes: 0
Reputation: 18142
You may want to try something like this:
bool ContainsEmptyCollection(IEnumerable<List<int>> collections)
{
foreach (var collection in collections)
{
if (collection.Count == 0)
return true;
}
return false;
}
Upvotes: 0
Reputation: 9959
var listoflists = new List<List<int>>;
bool allNonEmpty = listoflists.All(list => list.Any());
Upvotes: 1
Reputation: 12604
having nested lists can work well. you might want to look into the use of selectmany
but I think the code you want is something like:
var nested = new List<List<int>>();
// put data in it
nested.All((inner) => inner.Any());
Upvotes: 4