<p>Whats the best/easiest way to obtain a count of items within an IEnumerable collection without enumerating over all of the items in the collection?</p>

<p>Possible with LINQ or Lambda?</p>

[{'answer_id': 28761, 'author': 'Konrad Rudolph', 'author_id': 1968, 'author_profile': 'https://Stackoverflow.com/users/1968', 'pm_score': 4, 'selected': False, 'text': '<p>In any case, you have to loop through it. Linq offers the <code>Count</code> method:</p>\n\n<pre><code>var result = myenum.Count();\n</code></pre>\n'}, {'answer_id': 28762, 'author': 'Greg Hurlman', 'author_id': 35, 'author_profile': 'https://Stackoverflow.com/users/35', 'pm_score': 0, 'selected': False, 'text': "<p>Not possible with LINQ, as calling .Count(...) does enumerate the collection.  If you're running into the problem where you can't iterate through a collection twice, try this:</p>\n\n<pre><code>List&lt;MyTableItem&gt; myList = dataContext.MyTable.ToList();\nint myTableCount = myList.Count;\n\nforeach (MyTableItem in myList)\n{\n   ...\n}\n</code></pre>\n"}, {'answer_id': 28763, 'author': 'Brian Leahy', 'author_id': 580, 'author_profile': 'https://Stackoverflow.com/users/580', 'pm_score': 5, 'selected': True, 'text': '<p>You will have to enumerate to get a count.  Other constructs like the List keep a running count.</p>\n'}, {'answer_id': 28769, 'author': 'Joel Coehoorn', 'author_id': 3043, 'author_profile': 'https://Stackoverflow.com/users/3043', 'pm_score': 1, 'selected': False, 'text': "<p>There's also IList or ICollection, if you want to use a construct that is still somewhat flexible, but also has the feature you require.  They both imply IEnumerable.</p>\n"}, {'answer_id': 28919, 'author': 'ICR', 'author_id': 214, 'author_profile': 'https://Stackoverflow.com/users/214', 'pm_score': 4, 'selected': False, 'text': "<p>The solution depends on why you don't want to enumerate through the collection.</p>\n<p>If it's because enumerating the collection might be slow, then there is no solution that will be faster. You might want to consider using an ICollection instead if possible. Unless the enumeration is remarkably slow (e.g. it reads items from disk) speed shouldn't be a problem though.</p>\n<p>If it's because enumerating the collection will require more code then it's already been written for you in the form of the .Count() extension method. Just use MyEnumerable.Count().</p>\n<p>If it's because you want to be able to enumerate the collection after you've counted then the .Count() extension method allows for this. You can even call .Count() on a collection you're in the middle of enumerating and it will carry on from where it was before the count. For example:</p>\n<pre><code>foreach (int item in Series.Generate(5))\n{\n    Console.WriteLine(item + &quot;(&quot; + myEnumerable.Count() + &quot;)&quot;);\n}\n</code></pre>\n<p>will give the results</p>\n<blockquote>\n<p>0 (5)</p>\n<p>1 (5)</p>\n<p>2 (5)</p>\n<p>3 (5)</p>\n<p>4 (5)</p>\n</blockquote>\n<p>If it's because the enumeration has side effects (e.g. writes to disk/console) or is dependant on variables that may change between counting and enumerating (e.g. reads from disk) [N.B. If possible, I would suggest rethinking the architecture as this can cause a lot of problems] then one possibility to consider is reading the enumeration into an intermittent storage. For example:</p>\n<pre><code>List&lt;int&gt; seriesAsList = Series.Generate(5).ToList();\n</code></pre>\n<p>All of the above assume you can't change the type (i.e. it is returned from a library that you do not own). If possible you might want to consider changing to use an ICollection or IList (ICollection being more widely scoped than IList) which has a Count property on it.</p>\n"}, {'answer_id': 32131, 'author': 'Keith', 'author_id': 905, 'author_profile': 'https://Stackoverflow.com/users/905', 'pm_score': 0, 'selected': False, 'text': "<p>If you need to count and then loop you may be better off with a list.</p>\n\n<p>If you're using count to check for members you can use <code>Any()</code> to avoid enumerating the entire collection.</p>\n"}, {'answer_id': 4491774, 'author': 'Moustafa Deif', 'author_id': 548902, 'author_profile': 'https://Stackoverflow.com/users/548902', 'pm_score': 0, 'selected': False, 'text': '<p>The best solution -as I think is to do the following:</p>\n\n<ol>\n<li>using System.Linq.Dynamic;</li>\n<li>myEnumerable.AsQueryable().Count()</li>\n</ol>\n'}, {'answer_id': 6742821, 'author': 'Soundararajan', 'author_id': 851333, 'author_profile': 'https://Stackoverflow.com/users/851333', 'pm_score': 1, 'selected': False, 'text': '<p>It also depends on what you want to achieve by counting.. If you are interested to find if the enumerable collection has any elements, you could use </p>\n\n<p>myEnumerable.Any() over myEnumerable.Count() where the former will yield the first element and the later will yield all the elements. </p>\n'}, {'answer_id': 14139728, 'author': 'Mukesh', 'author_id': 1945660, 'author_profile': 'https://Stackoverflow.com/users/1945660', 'pm_score': 3, 'selected': False, 'text': '<p>Use this.</p>\n\n<pre><code>IEnumerable list =..........;\n\nlist.OfType&lt;T&gt;().Count()\n</code></pre>\n\n<p>it will return the count.</p>\n'}, {'answer_id': 40803854, 'author': 'Timmy Fuller', 'author_id': 2646050, 'author_profile': 'https://Stackoverflow.com/users/2646050', 'pm_score': 1, 'selected': False, 'text': '<p>An IEnumerable will have to iterate through every item. to get the full count. \nIf you just need to check if there is one or more items in an IEnumerable a more efficient method is to check if there are any. Any() only check to see there is a value and does not loop through everything. </p>\n\n<p>IEnumerable myStrings = new List(){"one","two", "three"};</p>\n\n<p>bool hasValues = myStrings.Any();</p>\n'}, {'answer_id': 65133304, 'author': 'Mike Meinz', 'author_id': 3146215, 'author_profile': 'https://Stackoverflow.com/users/3146215', 'pm_score': 0, 'selected': False, 'text': '<p>When I want to use the Count property, I use ILIST which implements IEnumerable and ICollection interfaces. The ILIST data structure is an Array. I stepped through using the VS Debugger and found that the .Count property below returns the Array.Length property.</p>\n<pre><code>IList&lt;string&gt; FileServerVideos = Directory.GetFiles(VIDEOSERVERPATH, &quot;*.mp4&quot;); \nif (FileServerVideos.Count == 0)\n    return;\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/28756', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2993/']