Browse by Tags

All Tags » LINQ » C# (RSS)

Reimplementing LINQ to Objects: Part 27 - Reverse

Time for a change of pace after the deep dive into sorting. Reversing is pretty simple... which is not to say there's nothing to discuss, of course. What is it? Reverse only has a single, simple signature: public   static IEnumerable<TSource>...
Posted by skeet | 17 comment(s)
Filed under: , ,

Reimplementing LINQ to Objects: Part 26d - Fixing the key selectors, and yielding early

I feel I need a voice over. "Previously, on reimplementing LINQ to Objects..." Well, we'd got as far as a working implementation of OrderedEnumerable which didn't have terrible performance - unless you had an expensive key selector....
Posted by skeet | 13 comment(s)
Filed under: , ,

Reimplementing LINQ to Objects: Part 26c - Optimizing OrderedEnumerable

Part 26b left us with a working implementation of the ordering operators, with two caveats: The sort algorithm used was awful We were performing the key selection on every comparison, instead of once to start with Today's post is just going to fix...
Posted by skeet | 8 comment(s)
Filed under: , ,

Reimplementing LINQ to Objects: Part 26b - OrderBy{,Descending}/ThenBy{,Descending}

Last time we looked at IOrderedEnumerable<TElement> and I gave an implementation we could use in order to implement the public extension methods within LINQ. I'm still going to do that in this post, but it's worth mentioning something else...
Posted by skeet | 10 comment(s)
Filed under: , ,

Reimplementing LINQ to Objects: Part 26a - IOrderedEnumerable

Implementing OrderBy/OrderByDescending/ThenBy/ThenByDescending isn't too bad, once you understand how the pattern works, but that's a slight conceptual leap. I'll take it one step at a time, first introducing some extra infrastructure (both...
Posted by skeet | 8 comment(s)
Filed under: , ,

Reimplementing LINQ to Objects: Part 25 - ToDictionary

This one ended up being genuinely easy to implement, although with lots of tests for different situations. What is it? ToDictionary has four overloads which look remarkably similar to the ones we used for ToLookup: public   static Dictionary<TKey...
Posted by skeet | 9 comment(s)
Filed under: , ,

Reimplementing LINQ to Objects: Part 24 - ToArray

This really is a simple one. So simple I might even find the energy to implement ToDictionary as well tonight... we'll see. (EDIT: Oops. It became slightly less simple in the end, as I came up with the third implementation. Oh well.) What is it? ToArray...
Posted by skeet | 9 comment(s)
Filed under: , ,

Reimplementing LINQ to Objects: Part 23 - Take/Skip/TakeWhile/SkipWhile

I genuinely expected these operators to be simple. At the time of this writing, I'm onto my third implementation of SkipWhile, struggling to find code which obviously expresses what's going on. I find it interesting how the simplest sounding operators...
Posted by skeet | 17 comment(s)
Filed under: , ,

Reimplementing LINQ to Objects: Part 22 - GroupJoin

Another operator that was decidedly easy to implement - partly as I was able to just adapt everything from Join, including the tests. 15 minutes for tests + implementation... and no doubt considerably long writing it up. What is it? After the complexity...
Posted by skeet | 7 comment(s)
Filed under: , ,

Reimplementing LINQ to Objects: Part 21 - GroupBy

Okay, after the brief hiatus earlier, we're ready to group sequences. What is it? GroupBy has eight overloads, using up to four type parameters and up to five normal parameters. Those of a sensitive disposition may wish to turn away now: public  ...
Posted by skeet | 11 comment(s)
Filed under: , ,

Reimplementing LINQ to Objects: Part 20 - ToList

This morning I started writing the tests for GroupBy, prior to implementing it. That turned out to be a pain - really I wanted an easy way of getting at each element of the result (i.e. each result group). If only I had the ability to convert an arbitrary...
Posted by skeet | 2 comment(s)
Filed under: , ,

Reimplementing LINQ to Objects: Part 19 - Join

You might expect that as joining is such a fundamental concept in SQL, it would be a complex operation to both describe and implement in LINQ. Fortunately, nothing could be further from the truth... What is it? Join has a mere two overloads, with the...
Posted by skeet | 6 comment(s)
Filed under: , ,

Reimplementing LINQ to Objects: Part 18 - ToLookup

I've had a request to implement Join, which seems a perfectly reasonable operator to aim towards. However, it's going to be an awful lot easier to implement if we write ToLookup first. That will also help with GroupBy and GroupJoin, later on....
Posted by skeet | 8 comment(s)
Filed under: , ,

Reimplementing LINQ to Objects: Part 17 - Except

I'm really pleased with this one. Six minutes ago (at the time of writing this), I tweeted about the Intersect blog post. I then started writing the tests and implementation for Except... and I'm now done. The tests are cut/paste/replace with...
Posted by skeet | 2 comment(s)
Filed under: , ,

Reimplementing LINQ to Objects: Part 16 - Intersect (and build fiddling)

Okay, this is more like it - after the dullness of Union, Intersect has a new pattern to offer... and one which we'll come across repeatedly. First, however, I should explain some more changes I've made to the solution structure... Building the...
Posted by skeet | 8 comment(s)
Filed under: , ,

Reimplementing LINQ to Objects: Part 15 - Union

I'm continuing the set-based operators with Union. I may even have a couple of hours tonight - possibly enough to finish off Intersect and Except as well... let's see. What is it? Union is another extension method with two overloads; one taking...
Posted by skeet | 5 comment(s)
Filed under: , ,

Reimplementing LINQ to Objects: Part 14 - Distinct

I'm going to implement the set-based operators next, starting with Distinct. What is it? Distinct has two overloads, which differ only in the simplest possible way: public   static IEnumerable<TSource> Distinct<TSource>(    ...
Posted by skeet | 7 comment(s)
Filed under: , ,

Reimplementing LINQ to Objects: Part 13 - Aggregate

EDIT: I've had to edit this quite a bit now that a second bug was discovered... basically my implementation of the first overload was completely broken :( Last night's tweet asking for suggestions around which operator to implement next resulted...
Posted by skeet | 6 comment(s)
Filed under: , ,

Reimplementing LINQ to Objects: Part 12 - DefaultIfEmpty

After the masses of code required for all the permutations of First/Last/etc, DefaultIfEmpty is a bit of a relief. What is it? Even this simple operator has two overloads : public   static IEnumerable<TSource> DefaultIfEmpty<TSource>...
Posted by skeet | 9 comment(s)
Filed under: , ,

Reimplementing LINQ to Objects: Part 11 - First/Single/Last and the ...OrDefault versions

Today I've implemented six operators, each with two overloads. At first I expected the implementations to be very similar, but they've all turned out slightly differently... What are they? We've got three axes of permutation here: {First,...
Posted by skeet | 9 comment(s)
Filed under: , ,
More Posts « Previous page - Next page »