.Net (6) ActionScript (1) Adobe (2) Apple (3) ASP Classic (1) ASP.Net (4) C# (1) ColdFusion (1) dasBlog (1) Flex (1) Game (1) Hacks (1) House (1) HTML (2) IIS (1) OData (1) Quotes (1) Random Thought (12) Recipes (1) Reviews (1) Silverlight (2) SQL (2) Standards (2) Vista (2) WCF (2) Windows (1) WPF (1) XML (1)
My Feed
I love linq. I would wager that most developers who have done much with it share a similar love.
It makes our lives easier. It eliminates writing copies amounts of loops, parsing xml, interacting with databases, you name it. It’s great.
When I run into a situation where linq does not have an obvious tie in, I start to get a little anxious, (pathetic I realize).
Today I was faced with a situation where I had a list of custom classes that I needed to sort.
This being a list, OrderBy was nowhere to be found.
So, I returned to my roots and started writing a compare for my class so that I could use the Sort command that is part of the List generic type. That is when it struck me that I could probably accomplish this using a lambda. It turns out that I was correct. Lambda’s were a perfect fit for this scenario. The code remained clean and concise and I was saved having to write additional methods just to accomplish a one-off scenario on what was a otherwise complete project.
Lamdas are a progression in C# past anonymous methods. In the code below, I show how the code would be implemented with separate function, an anonymous method and finally, with a Lambda expression.
First, the test class that we’ll be working with:
public class MyClass { public int propA { get; set; } public int propB { get; set; } public int propC { get; set; } }
Our test class:
public class Tester { public void Main() { List list = new List(); list.Add(new MyClass() { propA = 1, propB = 2, propC = 3 }); list.Add(new MyClass() { propA = 2, propB = 3, propC = 4 }); list.Add(new MyClass() { propA = 3, propB = 4, propC = 5 }); list.Add(new MyClass() { propA = 4, propB = 5, propC = 6 }); } }
Now then, we write a custom comparer. Not a lot of code, but you can imagine how this extrapolates over various properties for large classes.
public class Tester { private static int CompareByPropA(MyClass a, MyClass b) { return a.propA.CompareTo(b.propA); } public void Main() { List list = new List(); list.Add(new MyClass() { propA = 1, propB = 2, propC = 3 }); list.Add(new MyClass() { propA = 2, propB = 3, propC = 4 }); list.Add(new MyClass() { propA = 3, propB = 4, propC = 5 }); list.Add(new MyClass() { propA = 4, propB = 5, propC = 6 }); //Using the comparer built into the MyClass method list.Sort(CompareByPropA); } }
This time, we drop the custom comparer and implement the sort using an anonymous method.
public class Tester { public void Main() { List list = new List(); list.Add(new MyClass() { propA = 1, propB = 2, propC = 3 }); list.Add(new MyClass() { propA = 2, propB = 3, propC = 4 }); list.Add(new MyClass() { propA = 3, propB = 4, propC = 5 }); list.Add(new MyClass() { propA = 4, propB = 5, propC = 6 }); //Using an anonymous method list.Sort(delegate(MyClass a, MyClass b) { return a.propA.CompareTo(b.propA); }); } }
Finally, we rewrite the anonymous method into a Lambda. It is still easy to read and much more concise.
public class Tester { public void Main() { List list = new List(); list.Add(new MyClass() { propA = 1, propB = 2, propC = 3 }); list.Add(new MyClass() { propA = 2, propB = 3, propC = 4 }); list.Add(new MyClass() { propA = 3, propB = 4, propC = 5 }); list.Add(new MyClass() { propA = 4, propB = 5, propC = 6 }); //Using a Lambda list.Sort((a, b) => a.propA.CompareTo(b.propA)); } }
So, next time you go to write custom compare code, step back and see if you can do it a little more simply. If you are going to be using your comparison code in more than one place, then perhaps it is still a better idea to implement it using that route vs the lambda. If it is a one-off thing, then a lambda and some linq can be a beautiful thing!
.Net | C#
April 7, 2010 | Permalink | Disclaimer | Comments [0] | Trackback Related posts:OData = HotnessResponse.Redirect inside of a Try/CatchWPF: Element State BindingSilverlight AirlinesSilverlight, CoreCLR, DLR, oh My! « Review of Flex 4 In Action Main