If you want to test if your query's result contains at least one element, better use Any()
, like so:
Instead of...
CurrentUser.SentMatchRequest.FindAll(x => x.Id==OtherProfile.userId).Count==0
better use...
!CurrentUser.SentMatchRequest.FindAll(x => x.Id==OtherProfile.userId).Any()
If you really need the count, you can get it like this:
CurrentUser.SentMatchRequest.FindAll(x => x.Id==OtherProfile.userId).ToList().Count==0
The reason is, that IEnumerable<T>
can be used to iterate through a collection, but the number of elements in the collection remains unknown until you reach the end.
T get the count of elements, you must iterate through the whole collection, that's what happens with the ToList()
method, it forces LINQ to exactly do this.
If you only need to know, if the collection contains elements at all, this apporach is inefficient. Any()
however, does what you need: It checks, if the collection contains at least one element, so this operation does not have t iterate through the whole collection.