Click here to Skip to main content
15,868,016 members
Articles / Programming Languages / C#
Article

List Comprehensions for C# 2.0

Rate me:
Please Sign up or sign in to vote.
4.60/5 (14 votes)
21 May 2008Public Domain2 min read 46.3K   165   30   10
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:

C#
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:

C#
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:

C#
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:

C#
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, along with any associated source code and files, is licensed under A Public Domain dedication



Comments and Discussions

 
GeneralPoor Man's LINQ Pin
Qwertie19-May-08 13:11
Qwertie19-May-08 13:11 
GeneralRe: Poor Man's LINQ Pin
User 35433221-May-08 1:04
User 35433221-May-08 1:04 
Very nice, indeed. I have updated my article and added a link to your project.

Best regards,
Froh
GeneralInspiring Pin
Herre Kuijpers5-May-08 9:27
Herre Kuijpers5-May-08 9:27 
GeneralRe: Inspiring Pin
mariahayek5-May-08 11:54
mariahayek5-May-08 11:54 
QuestionLINQ? Pin
Judah Gabriel Himango5-May-08 8:38
sponsorJudah Gabriel Himango5-May-08 8:38 
AnswerRe: LINQ? Pin
User 3543325-May-08 9:02
User 3543325-May-08 9:02 
GeneralRe: LINQ? Pin
Roger Alsing5-May-08 9:42
Roger Alsing5-May-08 9:42 
GeneralRe: LINQ? Pin
User 3543325-May-08 9:46
User 3543325-May-08 9:46 
GeneralRe: LINQ? Pin
Roger Alsing5-May-08 9:51
Roger Alsing5-May-08 9:51 
GeneralRe: LINQ? Pin
mythteria5-May-08 20:20
mythteria5-May-08 20:20 

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

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