What are the difference between .Select, .Any, and .Count when using LINQ
Do you get a performance hit when using .Count just like in SQL select count(*)?
Does .Any perform faster ?
Thanks!
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Countneeds to iterate the entire collection, because it (obviously) needs to count the number of instances.Anyfinds the first occurrence & returns true or false. If there aren’t any, then it needs to iterate the whole collection to try to find, but if the first instance matches then it only needs to check the first instance.Selectis completely different. It is used to project a collection into another collection. It does not perform any checking or filtering.edit: In SQL terms,
Anyis likeExists, whileCountis likeCount(*).If I want to know whether there are any people on the street today, it is completely unnecessarily to count all the people and see if the number is >= 1. As soon as I find one person then I’m done.