Apr212006

The LINQ project, a preview of what’s coming in C#

I am not sure how I ended up on this page, but it was definitely a lucky accident. As the statement on the home page reads, the LINQ project is “a codename for a set of extensions to the .NET Framework that encompass language-integrated query, set, and transform operations. It extends C# and Visual Basic with native language syntax for queries and provides class libraries to take advantage of these capabilities.”

While quickly skimming through the pages, I read about the Standard Query Operators API that will allow querying of any .NET array or collection. Let’s see an example. We have a C# class named Product that has different attributes, such as UnitPrice and Name:

public class Product
{
	public int ProductID;
	public string Name;
	public string Category;
	public decimal UnitPrice;
	public int UnitsInStock;
}

We set up a generic collection that holds a set of products. Let’s assume that there’s a function that returns a list populated from somewhere.

List<Product> products = GetProductList();

Using the System.Query.Sequence static class, and after building some delegate types, we can do things like:

IEnumerable<Product> x = products.Where(p => p.UnitPrice >= 10);

Which would be the equivalent of issuing a WHERE products.UnitPrice >= 10 SQL query. Or you can even order the result after filtering the list:

IEnumerable<Product> orderedProducts1 =
	products.
	Where(p => p.UnitPrice >= 10).
	OrderBy(p => p.Category).
	ThenByDescending(p => p.UnitPrice).
	ThenBy(p => p.Name);

Which would be the equivalent of the SQL statement WHERE products.UnitPrice >= 10 ORDER BY p.Category, p.UnitPrice DESC, p.Name

While not 100% confirmed, this language feature seems to be a strong candidate to join the set of new exciting functionalities that will be available on C# 3.0. Can’t wait!

Bookmark and Share

Related posts:

  1. New fun project: a C++ Web Framework


Leave a Comment

1 Comment to "The LINQ project, a preview of what’s coming in C#"

  1. Mar012007 at 12:58 am

    atentaten [Visitor] wrote:

    That is nice. Note that Coldfusion has supported queries of complex objects for years. Queries of queries rock.

 
Powered by Wordpress and MySQL. Clauz's design for by Cricava