检查IEnumerable <list <int>&gt; </list <int>中的每个项目

时间:2012-04-26 14:03:05

标签: c# .net generics

目前我有一些类似下面的代码,基于NoOfRows属性返回是否所有数据都已输入到列表中:

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;
            }

我认为重构这段代码并拥有List<List<int>> or a Dictionary<int,List<int>>可能会更好,但是我如何才能返回上面的内容来返回集合中的每个列表是否都有内容?

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

5 个答案:

答案 0 :(得分:6)

如果您只想知道任何子列表是否为空,请尝试以下操作:

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());

这将检查列表列表中是否存在没有项目的列表。

但是,如果您希望这些子列表的索引为空,则只需将Any()的结果存储在所有子列表中:

// will return a lists of bools for Any() applied to each sub-list
var listStates = values.Select(l => l.Any()).ToList();

然后,值为false的任何结果的索引将是没有任何项目的列表。或者,您也可以使用直接传递索引的Select特殊形式。

答案 1 :(得分:4)

拥有嵌套列表可以很好地工作。您可能希望了解selectmany

的用法

但我认为您想要的代码类似于:

var nested = new List<List<int>>();
// put data in it

nested.All((inner) => inner.Any());

答案 2 :(得分:1)

var listoflists = new List<List<int>>;
bool allNonEmpty = listoflists.All(list => list.Any());

答案 3 :(得分:0)

你可能想尝试这样的事情:

    bool ContainsEmptyCollection(IEnumerable<List<int>> collections)
    {
        foreach (var collection in collections)
        {
            if (collection.Count == 0)
                return true;
        }

        return false;
    }

答案 4 :(得分:0)

你的意思是这样的:

        List<List<int>> list = new List<List<int>>();
        bool anyNonEmpty = list.Any(x => x.Any());
相关问题