-1
IEnumerable<DirectoryInfo> subDirs = di.EnumerateDirectories();
if (subDirs.Count() > 0)

I'm getting error on the Count() and I have in top of form1 using System.IO; already.

Error 2 '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?)

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
Daniel Lip
  • 3,867
  • 7
  • 58
  • 120

1 Answers1

1

Count<T>() is an extension method for IEnumerable<T>. To use it, you should add:

using System.Linq;

Extension Methods
The most common extension methods are the LINQ standard query operators that add query functionality to the existing System.Collections.IEnumerable and System.Collections.Generic.IEnumerable<T> types.
To use the standard query operators, first bring them into scope with a using System.Linq directive.
Then any type that implements IEnumerable<T> appears to have instance methods such as GroupBy<TSource, TKey>, OrderBy<TSource, TKey>, Average, and so on. You can see these additional methods in IntelliSense statement completion when you type "dot" after an instance of an IEnumerable<T> type such as List<T> or Array.

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • To the downvoter, Since the quoted part couldn't post in comments and reading it may help future readers to have a better understanding of problem, I post it as answer. Instead of downvote, you can vote to close the question if you think the question is off-topic. – Reza Aghaei Mar 12 '16 at 22:43
  • 1
    I closed as duplicate, but I'm upvoting your answer since it addresses the problem (although I'd rather we didn't have such blatant duplicates). – Douglas Mar 12 '16 at 22:45
  • You shouldn't have answered, you should have closed it as a dupe instead. – Claus Jørgensen Mar 12 '16 at 23:03
  • @ClausJørgensen I couldn't find a duplicate. Also that part part was really helpful and couldn't post as comment because of character counts. Anyway, I think donwvotes to this answer are really unfair :) – Reza Aghaei Mar 12 '16 at 23:04
  • You couldn't find a dupe, yet Google lists a 2008, and 2010 answers as the two top results. Neither you or the OP did any research it would appear. – Claus Jørgensen Mar 12 '16 at 23:11
  • @ClausJørgensen And how the qouted part is not result of a research? Can you show me an answer that contains the quoted part? (Which I believe is really helpful) – Reza Aghaei Mar 12 '16 at 23:15
  • @TomBlodget The Jon Skeet answer is good ant that's enough for that thread. When this answer gets downvotes here, surely if I move it to the other thread downvoters will arrange hard attacks just because there is a good answer there. Anyway thank you for your comment. I believe the answer doesn't deserve negative score and it's a good one. – Reza Aghaei Mar 13 '16 at 02:06
  • Jon Skeet also gives a better answer than you, since what OP in this question really asked for what is in fact `.Any()`, which you didn't bother to consider in your answer. – Claus Jørgensen Mar 13 '16 at 09:25
  • 2
    @ClausJørgensen: The onus is on the OP (and the community at large) to avoid duplicates, not on the answerers. – Douglas Mar 13 '16 at 09:44
  • @Douglas The answers are part of the community. Any long term SO user with knowledge in a area, would know which questions are obvious dupes, such as this one. This kind of answer is more about FGITW and scoring up votes. Even if it wasn't a dupe, this is still a rather poor answer; good answers should answer the actual problem, not the question. – Claus Jørgensen Mar 13 '16 at 10:07
  • @ClausJørgensen Pointing to `Any` can be at its best as a side note and surely it doesn't make any difference in reaction of who downvoted the answer. The OP really wants to know where is the Count method and the post completely answers his question and points to an official document to teach him about extension methods which is at least as good as poining to `Any`. – Reza Aghaei Mar 13 '16 at 10:19
  • @ClausJørgensen If you compare the time of comment and the time of answer you will find that it's not about FGITW. The taken time is used to find and read an official document (Which you claimed is not result of a researach) and trying to post as comment but couldn't and decided to post it as answer because its useful for future readers. – Reza Aghaei Mar 13 '16 at 10:22