The LINQ / Enumerable Connection

LINQ is a major part of .NET development, and you may already know that C# and VB both support a good number of LINQ query operators.  Using these operators, we can build expressions such as the following:

string[] currentVideoGames = {"Morrowind", "Uncharted 2",
        "Fallout 3", "Daxter", "System Shock 2"};

var subset = from game in currentVideoGames
             where game.Contains(" ")
             orderby game
             select game;

While the query operators are very helpful (and should be used when possible), always remember that they are just shorthand notations for the underlying LINQ object model.  The System.Core.dll assembly defines a key class type within the System.Linq namespace called Enumerable.  This class is little more than a set of extension methods which add new functionality to anything implementing IEnumerable<T>. 

As it turns out, these extension methods typically require you to specify some number of delegates (most often, Func<>) which will specify methods to be called when processing the data.  Thus, the previous LINQ query could be written as so:

string[] currentVideoGames = {"Morrowind", "Uncharted 2",
    "Fallout 3", "Daxter", "System Shock 2"};

// Build a query expression using extension methods
// granted to the Array via the Enumerable type.
var subset = currentVideoGames.Where(game => game.Contains(" "))
  .OrderBy(game => game).Select(game => game);

Here, we are opting to use lambdas to hide the use of the underlying delegate objects, however we could use them directly, in conjunction with anonymous method syntax.

string[] currentVideoGames = {"Morrowind", "Uncharted 2",
        "Fallout 3", "Daxter", "System Shock 2"};

// Build the necessary Func<> delegates using anonymous methods.
Func<string, bool> searchFilter =
  delegate(string game) { return game.Contains(" "); };
Func<string, string> itemToProcess = delegate(string s) { return s; };

// Pass the delegates into the methods of Enumerable.
var subset = currentVideoGames.Where(searchFilter)
  .OrderBy(itemToProcess).Select(itemToProcess);

To be sure, this is a much more complex version of the initial LINQ query, but the output is identical.  So why would one bother to use this underlying object model?  Simply put, Enumerable defines a number of methods for which there is no corresponding C# / VB operator. Again, while you should make use of the query operators where possible (just to keep your life simple), always be aware that there is a richer object model in the background.


Posted by: Andrew Troelsen
Posted on: 3/22/2010 at 9:20 AM
Tags:
Categories: .NET
Actions: E-mail | Kick it! | DZone it! | del.icio.us
Post Information: Permalink | Comments (0) | Subscribe to this BlogRSS comment feed
Comments are closed
Contact Us 651-994-8558 1-800-866-9884
Home | Training | Curriculum | Course Finder | Schedule | Enroll | Twin Cities Java User Group | Consulting | Foundation | Jobs | About Us | Our Story | Press Room | Instructors | President | Map & Directions | Sitemap

Java Training | JSF / Struts / Spring / Hibernate Training | Java Power Tools Training | .NET 4.0 & Visual Studio 2010 Training | .NET 3.5 and Visual Studio 2008 Training | .NET 2.0 and Visual Studio 2003 Training | Prism / MVVM / MEF Training | Microsoft Web Development Training | Cloud Computing Training | Ajax / Web Services / XML Training | Groovy and Grails Training | SQL Server 2008 Training | SQL Server 2005 Training | Mobile Development Training | SharePoint 2010 Training | SharePoint 2007 Training | Agile, Process, Analysis & Design Training | Arch/Design Patterns Training | Microsoft Official Curriculum Training | Web Development Training | Ruby Training | Rational Application Developer (RAD) Training | WebSphere Application Server Training | WebSphere Portal Training | WebLogic Training | Boot Camp Training | Project Management Training | C++ Training | Metro / WinRT / Windows 8 Development Training | Retired

Intertech delivers training on-site and virtually serving cities including Phoenix, AZ | San Francisco, CA | Los Angeles, CA | San Diego, CA | San Jose, CA | Washington, DC | Chicago, IL | Orlando, FL | Boston, MA | Duluth, MN | Minneapolis St. Paul, MN | Rochester, MN | Raleigh-Durham, NC | New York, NY | Philadelphia, PA | Austin, TX | Dallas, TX | Houston, TX | Seattle, WA.