Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IEnumerable doesn't have a Count method

I have the following method:

public bool IsValid {   get { return (GetRuleViolations().Count() == 0); } }  public IEnumerable<RuleViolation> GetRuleViolations(){   //code here } 

Why is it that when I do .Count() above it is underlined in red?

I got the following error:

Error 1 'System.Collections.Generic.IEnumerable' does not contain a definition for 'Count' and no extension method 'Count' accepting a first argument of type 'System.Collections.Generic.IEnumerable' could be found (are you missing a using directive or an assembly reference?) c:\users\a\documents\visual studio 2010\Projects\NerdDinner\NerdDinner\Models\Dinner.cs 15 47 NerdDinner

like image 710
aherlambang Avatar asked Apr 17 '10 17:04

aherlambang


People also ask

Does IEnumerable have count?

IEnumerable doesn't have a Count method.

How do you count in IEnumerable?

IEnumerable has not Count function or property. To get this, you can store count variable (with foreach, for example) or solve using Linq to get count.

What inherits from IEnumerable?

ICollection inherits from IEnumerable. You therefore have all members from the IEnumerable interface implemented in all classes that implement the ICollection interface.

What is IEnumerable and what significance does it hold?

What is IEnumerable in C#? IEnumerable in C# is an interface that defines one method, GetEnumerator which returns an IEnumerator interface. This allows readonly access to a collection then a collection that implements IEnumerable can be used with a for-each statement.

How to count IEnumerable values without iterating?

IEnumerable cannot count without iterating. Under "normal" circumstances, it would be possible for classes implementing IEnumerable or IEnumerable<T>, such as List<T>, to implement the Count method by returning the List<T>.Count property. However, the Count method is not actually a method defined on the IEnumerable<T> or IEnumerable interface.

Is there a class-specific IEnumerable count method in Java?

However, the Count method is not actually a method defined on the IEnumerable<T> or IEnumerable interface. (The only one that is, in fact, is GetEnumerator.) And this means that a class-specific implementation cannot be provided for it. Rather, Count it is an extension method, defined on the static class Enumerable.

How to increase the performance of IEnumerable<T>?

So your best bet is to use the Count () extension method on your IEnumerable<T> object, as you will get the best performance possible that way. Show activity on this post. Just adding extra some info: The Count () extension doesn't always iterate.

How to count the number of elements in a LINQ list?

Any () or Count () methods in Linq work only for generic types. instead. IEnumeration does not have a method called Count (). It's just a kind of "sequence of elements". Use for example List if you explicitly need the number of elements.


1 Answers

You add:

using System.Linq; 

at the top of your source and make sure you've got a reference to the System.Core assembly.

Count() is an extension method provided by the System.Linq.Enumerable static class for LINQ to Objects, and System.Linq.Queryable for LINQ to SQL and other out-of-process providers.

EDIT: In fact, using Count() here is relatively inefficient (at least in LINQ to Objects). All you want to know is whether there are any elements or not, right? In that case, Any() is a better fit:

public bool IsValid {   get { return !GetRuleViolations().Any(); } } 
like image 153
Jon Skeet Avatar answered Sep 27 '22 21:09

Jon Skeet