Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><code>List&lt;T&gt;</code> has a built-in function called <code>GetRange()</code> which was made specifically for what you're trying to do. It's extremely fast and doesn't need Linq, casting, etc...</p> <pre><code>List&lt;string&gt; prodASINs = dc.aboProducts.Select(a =&gt; a.asin).ToList(); for(int i = 0; i &lt; prodASINs.Count; i += 10) { List&lt;string&gt; buffer = prodASINs.GetRange(i, 10); // do something with buffer } </code></pre> <p>That's it. Very simple.</p> <hr> <p>Test results: <code>GetRange</code> vs. <code>Slice</code> vs. <code>Linq</code> with 5000 strings in <code>List&lt;string&gt;</code> As you can clearly see, the Skip/Take approach using Linq is over 383 times slower than <code>Slice&lt;T&gt;()</code> and 4,736 times slower than <code>GetRange()</code></p> <p>==================================================================================</p> <pre><code>GetRange took on average 168 ticks Slice took on average 2073 ticks Linq took on average 795643 ticks </code></pre> <p>Test method used (try it yourself): </p> <pre><code>private static void GetRangeVsSliceVsLinq() { List&lt;string&gt; stringList = new List&lt;string&gt;(); for (int i = 0; i &lt; 5000; i++) { stringList.Add("This is a test string " + i.ToString()); } Stopwatch sw = new Stopwatch(); long m1 = 0, m2 = 0, m3 = 0; for (int x = 0; x &lt; 10; x++) { Console.WriteLine("Iteration {0}", x + 1); Console.WriteLine(); sw.Reset(); sw.Start(); for (int i = 0; i &lt; stringList.Count; i += 10) { List&lt;string&gt; buffer = stringList.GetRange(i, 10); } sw.Stop(); Console.WriteLine("GetRange took {0} msecs", sw.ElapsedMilliseconds); Console.WriteLine("GetRange took {0} ticks", sw.ElapsedTicks); m1 += sw.ElapsedTicks; sw.Reset(); sw.Start(); string[] sliceArray = stringList.ToArray(); for (int i = 0; i &lt; sliceArray.Length; i += 10) { List&lt;string&gt; buffer = sliceArray.Slice(i, 10).ToList(); } sw.Stop(); Console.WriteLine("Slice took {0} msecs", sw.ElapsedMilliseconds); Console.WriteLine("Slice took {0} ticks", sw.ElapsedTicks); m2 += sw.ElapsedTicks; sw.Reset(); sw.Start(); var count = stringList.Count(); for (var i = 0; i &lt; count; i++) { var buffer = stringList.Skip(i * 10).Take(10).ToList(); } sw.Stop(); Console.WriteLine("Skip/Take took {0} msecs", sw.ElapsedMilliseconds); Console.WriteLine("Skip/Take took {0} ticks", sw.ElapsedTicks); m3 += sw.ElapsedTicks; Console.WriteLine(); } Console.WriteLine(); Console.WriteLine("GetRange took on average {0} ticks", m1 / 10); Console.WriteLine("Slice took on average {0} ticks", m2 / 10); Console.WriteLine("Linq took on average {0} ticks", m3 / 10); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload