Click here to Skip to main content
Click here to Skip to main content

List Comprehensions for C# 2.0

, 21 May 2008 Public Domain
Rate this:
Please Sign up or sign in to vote.
Writing nice list comprehensions for C# 2.0.

Introduction

List comprehensions are useful things to express list operations, like filtering, in a concise way. Coming from functional languages, they enter the mainstream by being popularized through languages like Python. Basically, a list comprehension says "take every object from a source collection, calculate a result from it (if it matches a certain criteria), and give me back an enumeration with all the results". This feature is also wildly popularized with C# 3.0's Linq, which extends list comprehensions by offering even more features. If you are already using C# 3.0, have a look at Linq (with LinqBridge, you can even use it with .NET Framework 2.0).

If you're stuck with VS 2005 (no C# 3.0 for you), you can at least use my implementation of list comprehensions that use a fluent interface. Its main feature is its simplicity (basically, all in one class), and it being public domain, you can just grab it, add it to your project, and pretend you wrote it on your own, without asking your software project manager for permission of adding another library.

But don't stop here, also have a look at Qwertie's Poor Man's Linq if you can afford to, this is a C# 2.0 library that implements a lot of Linq to objects features.

Background

In typical programs, we always see an assortment of foreach loops for processing the contents of an enumerable, for example, for extracting a list of IDs from a list of domain objects:

public IList<Guid> GetIds(IEnumerable<Customer> customers)
{
    List l = new List<Guid>();
    foreach (Customer cust in customers)
         l.Add(cust.Id);
    return l;
)

And, this just is performing an operation for one object without selecting.

Using the code

Using the Comprehension class, we can replace a call to GetIds() from above, to:

IList<Guid> ids = Comprehension.From(customers)
      .Select(delegate(Customer c){return cust.Id;})
      .AsList<Guid>();

In case we don't want to get all the customer IDs in the output, but only good customers' IDs, we just mix in a Where:

IEnumerable<Guid> ids = Comprehension.From(customers)
      .Select(delegate(Customer c){return cust.Id;})
      .Where(delegate(Customer c){return c.Complaints < 5;}
      .AsEnumerable<Guid>();

Oh, and did you see we have a choice of output as a full IList<> or as an IEnumerable<>?

Points of interest

You can also use this as a method to work around generic containers not being covariant. If you have a list of Customer objects but want to feed it into a method which expects a list of DomainObjects (the latter being the base class for the former), you can just use the Comprehension as an easy way to copy the elements over to a new container:

IEnumerable<DomainObject> ids = Comprehension.From(customers)
    .AsEnumerable<DomainObject>();

History

This is an updated version of this article that (hopefully) explains list comprehensions better and provides some of the information I received in the comments. Thank you for your input, people Smile | :) It will be my last article on C# 2.0 because Roger Aisling made me switch to 2008, programming in C# 3.0, targeting .NET 2.0 for the production code and .NET 3.5 (with Rhino Mocks 3.5 beta) for the test cases.

License

This article, along with any associated source code and files, is licensed under A Public Domain dedication

Share

About the Author

Frohwalt Egerer
Software Developer Egerer + Tandogan GmbH
Germany Germany
No Biography provided

Comments and Discussions

 
GeneralPoor Man's LINQ PinmemberQwertie19-May-08 14:11 
GeneralRe: Poor Man's LINQ PinmemberFrohwalt Egerer21-May-08 2:04 
GeneralInspiring PinmemberHerre Kuijpers5-May-08 10:27 
GeneralRe: Inspiring Pinmembermariahayek5-May-08 12:54 
QuestionLINQ? PinmemberJudah Himango5-May-08 9:38 
AnswerRe: LINQ? PinmemberFrohwalt Egerer5-May-08 10:02 
GeneralRe: LINQ? PinmemberRoger Alsing5-May-08 10:42 
You can use Linq in C#2 , if you just use the C#3 compiler.(but compiling for .net 2)
 
I've made a mini port of Linq to objects for C#2
There is also a much much more complete project "Linq Bridge" that contains pretty much all of Linq to objects.
 
http://rogeralsing.com/2008/03/31/linq-to-objects-for-net-2-available/[^]
(link to linq bridge is in there to)
 
Even if you use the C#2 compiler to compile your code, you can still just rip out the code from Linq Bridge and use their complete set of list comprehensions.
GeneralRe: LINQ? PinmemberFrohwalt Egerer5-May-08 10:46 
GeneralRe: LINQ? PinmemberRoger Alsing5-May-08 10:51 
GeneralRe: LINQ? Pinmembermythteria5-May-08 21:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141216.1 | Last Updated 21 May 2008
Article Copyright 2008 by Frohwalt Egerer
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid