Possible Duplicate:
Which method performs better: .Any() vs .Count() > 0?可能重复:哪种方法表现更好:.Any()vs .Count()> 0?
I just wonder why should I use Any()
instead of Count()
?, if we took the msdn example :
我只是想知道为什么我应该使用Any()而不是Count()?,如果我们采用msdn示例:
class Pet
{
public string Name { get; set; }
public int Age { get; set; }
}
class Person
{
public string LastName { get; set; }
public Pet[] Pets { get; set; }
}
public static void AnyEx2()
{
List<Person> people = new List<Person>
{ new Person { LastName = "Haas",
Pets = new Pet[] { new Pet { Name="Barley", Age=10 },
new Pet { Name="Boots", Age=14 },
new Pet { Name="Whiskers", Age=6 }}},
new Person { LastName = "Fakhouri",
Pets = new Pet[] { new Pet { Name = "Snowball", Age = 1}}},
new Person { LastName = "Antebi",
Pets = new Pet[] { }},
new Person { LastName = "Philips",
Pets = new Pet[] { new Pet { Name = "Sweetie", Age = 2},
new Pet { Name = "Rover", Age = 13}} }
};
// Determine which people have a non-empty Pet array.
IEnumerable<string> names = from person in people
where person.Pets.AsQueryable().Any()
select person.LastName;
foreach (string name in names)
Console.WriteLine(name);
/* This code produces the following output:
Haas
Fakhouri
Philips
*/
}
What if I used :
如果我使用了怎么办
IEnumerable<string> names = from person in people
where person.Pets.Count() > 0
select person.LastName;
It will give same result ! , (I don't think it created for shortness or something) , is there any feature for Any()
??
它会给出相同的结果! ,(我不认为它是为了简短或其他东西而创建的),是否有Any()的任何功能?
4 个解决方案
#1
17
Any
just checks if the sequence contains at least one element, while Count
needs to iterate over all elements. That's the difference. A classic scenario where Any
is preferred over Count
is this:
任何只是检查序列是否包含至少一个元素,而Count需要迭代所有元素。这就是区别。 Any优先于Count的经典场景是:
if (sec.Count() > 0)
vs
VS
if (sec.Any())
#2
5
Depending on exactly what implementation of IEnumerable<>
is hiding behind the interface, Any
could potentially be vastly quicker than Count
. If for example there's actually LINQ-to-SQL, or some other database provider there, it could be the difference between checking a table for at least 1 record, or having to count every record in the database.
根据IEnumerable <>的具体实现隐藏在接口后面,Any可能比Count快得多。例如,实际上有LINQ-to-SQL或其他一些数据库提供程序,可能是检查一个表至少有1条记录,或者必须计算数据库中的每条记录。
However, to my mind, the much more important reason is that using Any()
expresses your INTENT better than checking for Count() > 0
. It asks "are there any items?" rather than "find out how many items there are. Is that number greater than zero". Which to you is the more natural translation of "are there any items?" ?
但是,在我看来,更重要的原因是使用Any()表达你的INTENT比检查Count()> 0更好。它询问“有没有任何项目?”而不是“找出有多少项目。这个数字是否大于零”。对你来说,“有没有任何物品更自然的翻译?” ?
#3
2
Actually, it depends.
实际上,这取决于。
If your collection is in the form of an IEnumerable, the Count() method will iterate through all elements, whereas Any() won't have to. So for enumerables, Any() will have a (potentially significant) performance benefit.
如果你的集合是IEnumerable的形式,Count()方法将迭代所有元素,而Any()将不必。因此,对于可枚举,Any()将具有(可能显着的)性能优势。
In your example, however, Pets is an array, and so you would be better off using .Length rather than .Count(). In that case, there will be no significant difference of performance.
但是,在你的例子中,Pets是一个数组,因此你最好使用.Length而不是.Count()。在这种情况下,性能没有显着差异。
#4
2
To get the count, the code has to traverse the entire sequence. On a long, lazy-executed sequences, this can take significant time. Since I only want to know if the sequence contains one or more elements, it’s computationally more efficient to use the Any() extension method.
要获得计数,代码必须遍历整个序列。在漫长的,懒惰执行的序列上,这可能需要很长时间。由于我只想知道序列是否包含一个或多个元素,因此使用Any()扩展方法在计算上更有效。
Read : Eric Lippert's Blog
阅读:Eric Lippert的博客
Also userfull to read : Count() and Count property
用户还可以阅读:Count()和Count属性
#1
17
Any
just checks if the sequence contains at least one element, while Count
needs to iterate over all elements. That's the difference. A classic scenario where Any
is preferred over Count
is this:
任何只是检查序列是否包含至少一个元素,而Count需要迭代所有元素。这就是区别。 Any优先于Count的经典场景是:
if (sec.Count() > 0)
vs
VS
if (sec.Any())
#2
5
Depending on exactly what implementation of IEnumerable<>
is hiding behind the interface, Any
could potentially be vastly quicker than Count
. If for example there's actually LINQ-to-SQL, or some other database provider there, it could be the difference between checking a table for at least 1 record, or having to count every record in the database.
根据IEnumerable <>的具体实现隐藏在接口后面,Any可能比Count快得多。例如,实际上有LINQ-to-SQL或其他一些数据库提供程序,可能是检查一个表至少有1条记录,或者必须计算数据库中的每条记录。
However, to my mind, the much more important reason is that using Any()
expresses your INTENT better than checking for Count() > 0
. It asks "are there any items?" rather than "find out how many items there are. Is that number greater than zero". Which to you is the more natural translation of "are there any items?" ?
但是,在我看来,更重要的原因是使用Any()表达你的INTENT比检查Count()> 0更好。它询问“有没有任何项目?”而不是“找出有多少项目。这个数字是否大于零”。对你来说,“有没有任何物品更自然的翻译?” ?
#3
2
Actually, it depends.
实际上,这取决于。
If your collection is in the form of an IEnumerable, the Count() method will iterate through all elements, whereas Any() won't have to. So for enumerables, Any() will have a (potentially significant) performance benefit.
如果你的集合是IEnumerable的形式,Count()方法将迭代所有元素,而Any()将不必。因此,对于可枚举,Any()将具有(可能显着的)性能优势。
In your example, however, Pets is an array, and so you would be better off using .Length rather than .Count(). In that case, there will be no significant difference of performance.
但是,在你的例子中,Pets是一个数组,因此你最好使用.Length而不是.Count()。在这种情况下,性能没有显着差异。
#4
2
To get the count, the code has to traverse the entire sequence. On a long, lazy-executed sequences, this can take significant time. Since I only want to know if the sequence contains one or more elements, it’s computationally more efficient to use the Any() extension method.
要获得计数,代码必须遍历整个序列。在漫长的,懒惰执行的序列上,这可能需要很长时间。由于我只想知道序列是否包含一个或多个元素,因此使用Any()扩展方法在计算上更有效。
Read : Eric Lippert's Blog
阅读:Eric Lippert的博客
Also userfull to read : Count() and Count property
用户还可以阅读:Count()和Count属性