是否有一个LINQ扩展或(一个合理的/有效集LINQ entensions的)确定集合是否至少有'X'的元素呢? [英] Is there a LINQ extension or (a sensible/efficient set of LINQ entensions) that determine whether a collection has at least 'x' elements?

查看:118
本文介绍了是否有一个LINQ扩展或(一个合理的/有效集LINQ entensions的)确定集合是否至少有'X'的元素呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有需要知道的集合不能为空或者只包含一个项目代码



在一般情况下,我想形式的扩展:



布尔collectionHasAtLeast2Items = collection.AtLeast(2);



我可以很容易地编写扩展,列举了收集和递增一个索引,直到我打的请求的大小,或用完的元素,但有一些已经在LINQ框架,做到这一点?我的想法(在什么来找我,顺序):



布尔collectionHasAtLeast2Items = collection.Take(2).Count之间的()== 2;



布尔collectionHasAtLeast2Items = collection.Take(2).ToList()==计数2。



这似乎工作,虽然采取更多的元素比集合行为中包含未定义(在文档中)的 Enumerable.Take方法,但是,它似乎做的一所期望的。



这不是最有效的解决方案,无论是列举一旦取元件,然后再次枚举算来,这是不必要的,或列举一旦取的元素,然后构造一个表,以获得计数财产不枚举-Y,因为我其实不愿意名单。



这不是漂亮,我总是不得不做出两个断言,首先取。'X',然后选中我居然收到'X',这取决于无证行为



或许我可以使用:



布尔collectionHasAtLeast2Items = collection.ElementAtOrDefault(2)= NULL;!



不过,这不是语义清晰。也许最好的是包装与方法名,这意味着我想要的。我假定这将是有效的,我还没有反映在代码。



一些其他的想法是使用最后(),但我明确不想通过整个集合枚举。



也许跳转(2)。任何( ),再次语义不完全明显,但比 ElementAtOrDefault(2)!= NULL 更好,但我会认为他们产生相同的结果?



有什么想法?


解决方案

 公共静态布尔ATLEAST< T>(这个IEnumerable的< T>源,诠释计数)
{
为ICollection的< //优化; T>
VAR genericCollection =源作为的ICollection< T> ;;
如果(genericCollection!= NULL)
返回genericCollection.Count> =计数;

//优化的ICollection
变种集合=源作为ICollection的;
如果(集合!= NULL)
返回collection.Count> =计数;

//一般情况下使用(VAR EN = source.GetEnumerator())
{
INT N = 0
;
,而(N<计数和放大器;&安培; en.MoveNext())N ++;
返回否==计数;
}
}


I have code that needs to know that a collection should not be empty or contain only one item.

In general, I want an extension of the form:

bool collectionHasAtLeast2Items = collection.AtLeast(2);

I can write an extension easily, enumerating over the collection and incrementing an indexer until I hit the requested size, or run out of elements, but is there something already in the LINQ framework that would do this? My thoughts (in order of what came to me) are::

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

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

Which would seem to work, though the behaviour of taking more elements than the collection contains is not defined (in the documentation) Enumerable.Take Method , however, it seems to do what one would expect.

It's not the most efficient solution, either enumerating once to take the elements, then enumerating again to count them, which is unnecessary, or enumerating once to take the elements, then constructing a list in order to get the count property which isn't enumerator-y, as I don't actually want the list.

It's not pretty as I always have to make two assertions, first taking 'x', then checking that I actually received 'x', and it depends upon undocumented behaviour.

Or perhaps I could use:

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

However, that's not semantically-clear. Maybe the best is to wrap that with a method-name that means what I want. I'm assuming that this will be efficient, I haven't reflected on the code.

Some other thoughts are using Last(), but I explicitly don't want to enumerate through the whole collection.

Or maybe Skip(2).Any(), again not semantically completely obvious, but better than ElementAtOrDefault(2) != null, though I would think they produce the same result?

Any thoughts?

解决方案

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

这篇关于是否有一个LINQ扩展或(一个合理的/有效集LINQ entensions的)确定集合是否至少有'X'的元素呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆