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

List Comprehensions for C# 2.0

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


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.


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)
    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;})

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;}

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)


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.


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

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 13:11 
GeneralRe: Poor Man's LINQ PinmemberFrohwalt Egerer21-May-08 1:04 
GeneralInspiring PinmemberHerre Kuijpers5-May-08 9:27 
GeneralRe: Inspiring Pinmembermariahayek5-May-08 11:54 
QuestionLINQ? PinmemberJudah Himango5-May-08 8:38 
I'd suggest a little more content to this kind of say, "here's what a list comprehension is, and here's how to use it." It's enough to put the article through, but is a little on the weak side.
I'd suggest talking a bit about the usefulness of comprehensions, and how this library relates to LINQ which seemingly does the same thing.


Tech, life, family, faith: Give me a visit.
From my latest post: "The world will call you weak, unintelligent, and foolish for following God. Don't worry about them. God's picked the foolish things in order to put the world's wisdom to shame."
The apostle Paul, modernly speaking: Epistles of Paul
Judah Himango

AnswerRe: LINQ? PinmemberFrohwalt Egerer5-May-08 9:02 
GeneralRe: LINQ? PinmemberRoger Alsing5-May-08 9:42 
GeneralRe: LINQ? PinmemberFrohwalt Egerer5-May-08 9:46 
GeneralRe: LINQ? PinmemberRoger Alsing5-May-08 9:51 
GeneralRe: LINQ? Pinmembermythteria5-May-08 20: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 | Mobile
Web02 | 2.8.140721.1 | Last Updated 21 May 2008
Article Copyright 2008 by Frohwalt Egerer
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid