Ignoring the way in which Linq can be processed by other query providers (for example, for a database), in the example you are referring to, we have two different approaches:
- First you get everything from the first listing, and then release everything from the second.
- Call the
Enumerable.Concat
method.
Well, what is Enumerable.Concat
? Since we ignore cases like Linq2SQL (which will most likely turn concat into UNION ALL
), what we care about here is the implementation of Linq2Objects.
Now you have more than one way to bypass the concat, but the Mono source (for example) finishes calling the check, and then:
static IEnumerable<TSource> CreateConcatIterator<TSource> (IEnumerable<TSource> first, IEnumerable<TSource> second) { foreach (TSource element in first) yield return element; foreach (TSource element in second) yield return element; }
In other words, the LINQ approach is a yield
approach.
Or, if not, it could be something very similar. We could (if we liked printing more) implement it as an IEnumerator<TSource>
implementation construct, but yield
saves us the hassle.
In general, LINQ is a set of handy tools that work well together. When it is a tool that works well, use them. When another tool works better, use it. When the LINQ-like tool is nice, but it is not included in what you have, write it yourself (just like we could do all the Linq2Objects stuff before Linq, which was not for us in .NET2.0) .
source share