Does Enumerable.Where in LINQ-to-objects preserve order?

Enumerable. Where will preserve the order of an IEnumerable) It does, however, include special implementations to handle certain types (such as arrays and List.

Enumerable. Where will preserve the order of an IEnumerable. This is not documented, so not a guarantee, however, the implementation just iterates through the IEnumerable sequentially - in effect, preserving order - though that order is reliant on the underlying IEnumerable's order.(For example, Where on a HashSet is not ordered, but it's because the HashSet's enumerable is unordered.) It does, however, include special implementations to handle certain types (such as arrays and List), so it's not impossible that a specific collection type, at some point in the future, might return results in a different order if this would be deemed a valuable improvement in terms of speed/perf.

/etc. The documentation never specifically gives an ordering guarantee.

It's not documented. Documentation of the LINQ operators is seriously lacking in many aspects, including when and when isn't order kept, when and when isn't the operation buffered, or what are the complexity guarantees. In this case, I don't mind depending on the implementation keeping the order, because that's what all practical implementations will do.

I recommend reading the Edulinq series by Jon Skeet where, before implementing the functionality, he explain what you should and what you should not expected of an operator.

The Where implementation is essentially as follows with some additional variable checking added in: public static IEnumerable Where(this IEnumerable source, Funct predicate) { foreach (T item in source) if (predicate(item)) yield return item; } As a result, assuming yield is not being done asynchronously or in parallel, it will retain the order. If your source is . AsParallel, all bets are off in terms of the order.

As a side note, I wish Where used Predicate instead of Func. . Net has a lot of cool stuff in it, but sometimes the standard library developers don't seem to realize it.

– recursive Aug 31 at 15:49 This is actually pretty far from the implementation in . NET 4 - though it would give the same results. There are quite a few custom iterator classes used in LINQ to Object's to implement Where efficiently on different underlying types.

– Reed Copsey Aug 31 at 15:49 @recursive: Doesn't make that big a difference now. With . NET 4 those are interchangeable.It did matter back when it came out though.

– R. Martinho Fernandes Aug 31 at 15:58 1 @recursive, they decided to go with Func rather than Predicate as it was more generic and re-usable. – Jim Wooley Aug 31 at 16:00 @Reed, understood, but the code above should give a good enough picture from a generalized perspective.

Also, the custom iterator classes seen in the IL are often generated by the compiler (from the yield keyword) and may not reflect the developer's thought process. Also, the Yield code spit is being modified for the next version of C# to reflect optimizations found in implementing async/await. – Jim Wooley Aug 31 at 16:01.

The decompilation of the LINQ where is: private static IEnumerable WhereIterator(IEnumerable source, Func predicate) { int num = -1; IEnumerator getenumerator = source.GetEnumerator(); while (getenumerator.MoveNext()) { TSource current = getenumerator. Current; num++; if (predicate(current, num)) { yield return current; } } } The decompilation of the System.Collections.Generic.List.MoveNext() has the code: if (this. Version == ts.

_version && this. Index Current = ts. _itemsthis.

Index; this. Index = this. Index + 1; return true; } Using these two together you can see that the the order will be preserved.

Of course Microsoft could change it in the future, but based on . NET 4.0, List. Where will be ordered.

The order is preserved using the Enumerable. Where method. There was a similar question asked on SO, and the first answer breaks down which methods preserve the order: Preserving order with LINQ.

It seems that it must preserve order, because it is able to function on infinite IEnumerables. Void Main() { foreach (var element in Count(). Where (i => i%2 == 1)) { // do something with all odd numbers } } IEnumerable Count() { int I = 0; while (true) yield return ++i; } I can't find this documented anywhere though.

1 Well, it could be wacky and produce them out of the order: 1,0,3,2,5,4,... and still work on infinite IEnumerables :P – R. Martinho Fernandes Aug 31 at 15:46.

Yes, it's going to be ordered according to the iterator. Since this isnt PLINQ, the enumerable is iterated over in order of element position of the enumerable - so if you implemented some kind of custom IEnumerator for your class, it would return in that ordering.

Enumerable. Where follows the ordering of elements returned by the enumerator generated by calling GetEnumerator on the underlying collection - looking at MSDN the enumerator on List is documented to follow the indexing behaviour.

Enumerable. Where follows the ordering of elements returned by the enumerator generated by calling GetEnumerator.

Without providing explicit OrderBy, the Where method cannot guarantee the order - otherwise, it would have been documented as doing such.

Declarative Data Parallelism is also known as Parallel LINQ (PLINQ) it is the final namespace that we are going to explore in the Parallel Extensions library. Linq contains the implementation of Parallel LINQ (PLINQ). It Enables LINQ developers to obtain multiple cores from their LINQ expressions by running any LINQ-to-objects query using data parallelism.

PLINQ fully supports all . NET query operators and has minimal impact to the existing LINQ model. First let’s show some basic LINQ code and remind ourselves of the way LINQ to Objects works.

…the following LINQ snippet would output the total count of the numbers between 1 and 1000 that are divisible by 5 (i.e. In other words, a LINQ-to-objects query is typically a chained series of method calls to extension methods that reside in System.Linq. Enumerable (in System.Core.

NET Framework v3.5) that act on IEnumerable by accepting delegates. The result of the calls ends up in the variable q, which in the example above would really be an IEnumerable. If you are not familiar with LINQ at all, please read my explanation of the new C# 3.0 and VB9 language features and how LINQ to objects works on my blog.

Can you spot the difference? We can potentially make LINQ-to-objects queries run faster and scale better on multi-core machines simply by using the Parallel Extensions library and applying the AsParallel extension method to the source! So, by injecting the AsParallel method we turned the source from an IEnumerable to an IParallelEnumerable and thus we also injected our own extension methods that reside in System.Linq.

ParallelEnumerable (in System.Threading. The class diagram in Figure 4 shows the five types involved in PLINQ. As you can see, IParallelEnumerable inherits from IEnumerable and hence can be used in all the places that IEnumerable can.

The ParallelEnumerable static class has almost 200 extensions methods that are the parallel equivalents of the methods in Enumerable. It is in the implementation of these extension methods that the query gets parallelised. Also note on the class diagram the static ParallelQuery class that has a few overloads for the (seemingly) magic AsParallel extension method.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions