是否有LINQ扩展或(一组合理/有效的LINQ扩展)确定集合是否至少具有'x'元素?

nic*_*s13 8 .net c# linq performance enumeration

我有代码需要知道集合不应该是空的或只包含一个项目.

一般来说,我想要一个表单的扩展:

bool collectionHasAtLeast2Items = collection.AtLeast(2);

我可以轻松地编写扩展,枚举集合并递增索引器,直到我达到所请求的大小,或者耗尽元素,但是LINQ框架中是否已经存在这样做?我的想法(按照发给我的顺序)是::

bool collectionHasAtLeast2Items = collection.Take(2).Count() == 2; 要么

bool collectionHasAtLeast2Items = collection.Take(2).ToList().Count == 2;

虽然采用比集合包含更多元素的行为没有定义(在文档中)Enumerable.Take方法,但它看起来像人们所期望的那样.

它不是最有效的解决方案,要么枚举一次取元素,再枚举再次计算它们,这是不必要的,或者枚举一次取元素,然后构造一个列表以获得不是枚举器的count属性-y,因为我实际上不想要列表.

它并不漂亮,因为我总是要做两个断言,首先取'x',然后检查我实际收到'x',这取决于无证件的行为.

或许我可以使用:

bool collectionHasAtLeast2Items = collection.ElementAtOrDefault(2) != null;

但是,这在语义上并不明确.也许最好的方法是使用方法名称来包装它,这意味着我想要的东西.我假设这将是有效的,我没有反映在代码上.

其他一些想法正在使用Last(),但我明确地不想枚举整个集合.

或者,也许Skip(2).Any(),再次在语义上并不完全明显,但更好ElementAtOrDefault(2) != null,虽然我认为它们会产生相同的结果?

有什么想法吗?

Tho*_*que 4

public static bool AtLeast<T>(this IEnumerable<T> source, int count)
{
    // Optimization for ICollection<T>
    var genericCollection = source as ICollection<T>;
    if (genericCollection != null)
        return genericCollection.Count >= count;

    // Optimization for ICollection
    var collection = source as ICollection;
    if (collection != null)
        return collection.Count >= count;

    // General case
    using (var en = source.GetEnumerator())
    {
        int n = 0;
        while (n < count && en.MoveNext()) n++;
        return n == count;
    }
}
Run Code Online (Sandbox Code Playgroud)