ienumerable<> count vs ienumerable.tolist()。计数
我试图找到列表是否为空。最有效的方法是什么。
IEnumerable<int> TestProp={get; set;}
该测试程序将通过另一种方法填充。当我尝试检查列表是否为空时,我必须至少做很多100次。 现在,我看到了它,据
var cnt=TestProp.ToList().Count>0;
我所知,iEnumerable
将尝试通过转换为Icollection来查找计数属性,但是我在这里寻找的是查找非空列表的最佳性能方法。我以某种方式在这里不需要tolist()
。
I am trying to find if a list is Empty. Whats the most efficient way.
IEnumerable<int> TestProp={get; set;}
this TestProb will be populated by another method. when I try to check if list is empty and I have to do this many times few 100s atleast.
Right now I see it as
var cnt=TestProp.ToList().Count>0;
I know IEnumerable
will try to find Count property by converting to ICollection but what I am looking here is best performant way for finding non empty list. I somehow feel ToList()
is not needed here.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以编写自己的扩展名
并
可以使用,
也可以使用布尔值进行比较,以便您不能这样使用,
因为
testprop?.any()
将返回nullable&lt; bool&bool&gt; < /代码>
you can write your own extension
and call
or you can use
but you must be compare with boolean value so you cannot use like this
because
TestProp?.Any()
will returnNullable<bool>
通常,答案是使用
.yany()
如果您想要一个更完整的答案,可以找到它在这里
In general, the answer is to use
.Any()
If you want a more complete answer you can find it here