This is my code for order the first N element randomly in a List :
int upper = 1;
if (myList.Count > 1)
{
Random r = new Random();
upper = Math.Min(maxNumberOfPackets, myList.Count);
for (int i = 0; i < upper; i++)
{
int randInd = r.Next(i, myList.Count);
var temp = myList[i];
myList[i] = myList[randInd];
myList[randInd] = temp;
}
}
well, now I have the “necessity” to use only Enumerable (for some reason; so I don’t want to convert it in a Enumerable).
For your knowledge, can I do the same with Enumerable? I think the pain is access to element at the X position…
I’m so curious…
IEnumerablerepresents a ‘forward only cursor’, which may return data that is streamed (from a database for instance). So to be able to order an enumerable (no matter whether it is random or not), does mean you need to cache it, simply because you need to have all values to be able to determine the ordering. That said, you can use theEnumerable.OrderByoperator for this, but again, it will do caching for you under the covers.