SingingEels : Development Community & Resource

Login

Articles

  • ADO.NET (2)
  • ASP.NET (36)
  • LINQ (4)
  • Security (2)
  • Silverlight (3)
  • SQL (7)
  • Standards (5)
  • WCF (2)

Syndication

  • Articles RSS
  • Blogs RSS

Contribute

  • Our Authors List
  • Member Sign-Up
  • Suggestions Box
ASP.NET Hosting with MS SQL 2008 – Click Here!

LINQ, Lambda and Extension Methods Work Together!

(Sep 27 2007 - 07:47:01 PM by Timothy Khouri) - [print blog post]
ASP.NET Hosting with MS SQL 2008 – Click Here!

Thanks to the new language features such as LINQ, Extension Methods and Lambda in .NET 3.5, working with arrays is as easy as one line. Just by including a reference to the System.Linq namespace in your .Net 3.5 project you automatically get so much functionality it's not even funny.

Let's take for example this very simple task and see how including these above language featuers cut the work by 80%! Assuming we have some sort of list (or in this case a simple array is fine) of "Sales" items, and we want to build a function that will return the number of sales in a particular zip code.

This is very common and simple functionality and it could be done like this:

public int GetNumberOfSalesByZip(string zipCode)
{
   int totalSales = 0;

   foreach (Sale saleItem in this.Sales)
   {
       if (saleItem.Zip == zipCode)
       {
           totalSales++;
       }
   }

   return totalSales;
}

There's nothing wrong with that code above. It's very simple and straight forward. But I mentioned above that we could drop these 5 lines of code down by 80% (meaning, I'm about to pull this functionality out in a one liner here)! Let's see how we would do this same function using LINQ, and Lambda (and Extension Methods which I'll explain in a minute):

public int GetNumberOfSalesByZip(string zipCode)
{
   return this.Sales.Count(saleItem => saleItem.Zip == zipCode);
}

There you have it, one line of code. Now, if you are an avid .NETer you'll be quick to point out that arrays DO NOT have a function called "Count". This is true, but that's where Extension Methods come in. Just by making a reference to System.Linq (a "using" statement in C# or a "imports" statement in VB.NET) we will automatically get functions added on to EVERY array, generic list or other type of "IEnumerable" class. Here are some sample functions that I love:

  • Any - Returns true if any items meet the criteria.
  • Average - Returns an average of the field specified.
  • Count - Pretty straight forward (as shown above).
  • Where - Just like SQL, you can return a subset based on a condition.
  • OrderBy - Also just like SQL.

These are just a handful of the many functions built in just by switching to .NET 3.5.

You must be logged in to add comments. If you have not already done so, you can create an account here. If you already are a member, you first need to login before you can comment.

Developer / Architect / Author

Blog Archives

  • November 2008 - (1)
  • October 2008 - (2)
  • September 2008 - (2)
  • August 2008 - (3)
  • July 2008 - (1)
  • June 2008 - (3)
  • May 2008 - (2)
  • April 2008 - (2)
  • March 2008 - (4)
  • February 2008 - (2)
  • December 2007 - (2)
  • November 2007 - (1)
  • October 2007 - (4)
  • September 2007 - (9)
  • August 2007 - (7)

Related Ads

SingingEels.com as of Jan 05 2009 - 08:08:19 PM - (0.0624984)