That is something from which I can get a graph?

I have it:

private IEnumerable _myList; 

I need to get a count of this object. I previously injected _myList into an array and got the length, but now we use the same bit of code with a different kind of object. It is still a type of collection (it is a strongly typed Subsonic Collection object) and everything works fine except for the bits we need to get the total number of elements in the object.

I tried to introduce it into CollectionBase and many other types, but nothing works, which will allow me to get .Count or .Length or something like that.

Can someone point me in the right direction?

EDIT: I do not use 3.5, I use 2. So, everything related to Linq will not work. Sorry for not posting this before.

0
source share
9 answers

Is it actually IEnumerable instead of IEnumerable<T> ? If so, LINQ will not help you directly. (You can use Cast<T>() as suggested elsewhere, but it will be relatively slow - in particular, it will not be optimized for implementations of IList / IList<T> .)

I suggest you write:

 public static int Count(this IEnumerable sequence) { if (sequence == null) { throw new ArgumentNullException("sequence"); } // Optimisation: won't optimise for collections which // implement ICollection<T> but not ICollection, admittedly. ICollection collection = sequence as ICollection; if (collection != null) { return collection.Count; } IEnumerator iterator = sequence.GetEnumerator(); try { int count = 0; while (iterator.MoveNext()) { // Don't bother accessing Current - that might box // a value, and we don't need it anyway count++; } return count; } finally { IDisposable disposable = iterator as IDisposable; if (disposable != null) { disposable.Dispose(); } } } 
+11
source

The System.Linq.Enumerable.Count extension method does this for a typed IEnumerable<T> . For untyped IEnumerable try making your own extension:

  public static int Count(this IEnumerable source) { if (source == null) { throw new ArgumentNullException("source"); } ICollection collectionSource = source as ICollection; if (collectionSource != null) { return collectionSource.Count; } int num = 0; IEnumerator enumerator = source.GetEnumerator(); //try-finally block to ensure Enumerator gets disposed if disposable try { while (enumerator.MoveNext()) { num++; } } finally { // check for disposal IDisposable disposableEnumerator = enumerator as IDisposable; if(disposableEnumerator != null) { disposableEnumerator.Dispose(); } } return num; } 
+10
source

If you are using .NET 3.5, you can use Enumerable.Count () to get the counter from any IEnumerable<T> .

This will not work with non-generic IEnumerable , though - this requires IEnumerable<T> .

This should work, however, since the Subsonic collection classes implement the appropriate interfaces for you. You will need to change your definition from IEnumerable to IEnumerable<MyClass> .

+4
source

LINQ provides an extension method to Count() .

 using System.Linq; 

...

 var count = _myList.Count(); 
+1
source

If you include the System.Linq namespace, IEnumerable<T> has an extension () extension method . You can write your own extension method to get it on a non-generic version. Please note that this method will use field value types, so if this could be a problem for you, go to the Jon Skeet solution . It is easier.

 public static int Count(this IEnumerable enumerable) { int count = 0; foreach(object item in enumerable) { count++; } return count; } 
+1
source

The type you are using is IEnumerable, which does not have the Count property. But the general equivalent of IEnumerable (T) has the Count property.

The obvious solution is to use IEnumerable (T), but if you cannot, you can do something like this:

 _myList.Cast<MyListItemType>().Count() 

Listing is an easy way to convert IEnumerable to IEnumerable (SomeType), but obviously this is not the best way to get a performance counter.

If performance is a factor, I would just skip the values ​​to get the score, if you don't know that the base collection has the Count property (see Jon Skeet answer ...).

+1
source

How about calling .ToList() ?

0
source

If the base object implements ICollection, you can use the .Count () property.

0
source

I need the same thing and I created an IEnumerableList. The reason is that I did not want to evaluate every time I needed an account through the entire enumerated object, as was done using the Count () extension method.

More on this here: http://fknet.wordpress.com/2010/08/11/string-formatwith-extension/

0
source

Source: https://habr.com/ru/post/1302916/


All Articles