Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

List Comprehensions for C# 2.0

0.00/5 (No votes)
21 May 2008 1  
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 :-) 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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here