ServicesResourcesConferencesOur TeamWeblogsAboutContact
   

Subscriptions

Post Categories

News

My new books

My Blogroll

INETA UG Leaders

Affiliations

News

Archives

Christian Nagel's OneNotes

.NET Training, Consulting, Coaching & Development


LINQ Part 1 - Filtering & Sorting Object Lists

Today accessing object lists, databases, and XML requires a different syntax for every of these technologies. LINQ (Language Integrated Query) makes it possible to use the same syntax accessing all of these technologies.

In this and some follow-on blog entries I'm showing how LINQ can be used to access object lists, databases, and XML.

With the sample application I have a list of racers List<Racer>. The Racer class is a very simple class that contains the name of the racer and the number of wins. With the result I just want the racers who have won more than three races, and the result should be sorted by the number of wins.

The traditional way (using .NET 2.0) to filter objects from a List<T> is by using the FindAll method passing a predicate. The predicate can be implemented using an anonymous method as shown:

List<Racer> winners = racers.FindAll(
   delegate(Racer r)
   {
      return r.Wins > 3;
   });

Sorting the result is done with the Sort method of List<T>. One overload of Sort accepts a generic delegate Comparison<T> that can be implemented as anonymous method:

winners.Sort(
   delegate(Racer r1, Racer r2)
   {
      return r2.Wins.CompareTo(r1.Wins);
   });

Displaying the resulting collection is done by using the ForEach method passing an Action<T> delegate:

winners.ForEach(
   delegate(Racer r)
   {
      Console.WriteLine("{0}, {1}", r.Name, r.Wins);
   });

C# 2.0 with the generic list class and using anonymous methods makes the filtering and sorting very easy (as soon as you are used to the syntax for anonymous methods). It's even simpler using the LINQ query expression (C# 3.0). The same result shown before can be done with a simple from in where orderby select:

var winners = from r in racers
   where r.Wins > 3
   orderby r.Wins descending
   select r;

foreach (Racer r in winners)
{
   Console.WriteLine("{0}, {1}", r.Name, r.Wins);
}

With the next blog entries I'm showing how to do the same query using a database and XML sources, and what's behind the scenes.

Christian

posted on Saturday, March 11, 2006 10:06 AM

# Link Listing - March 12, 2006 @ Monday, March 13, 2006 5:40 AM

Debugging AJAX apps, part 5 - XmlHttpRequest in IE /
FireFox [Via: Rumen Stankov ]
Debugging AJAX...
Christopher Steen

# LINQ Part 2 - Filtering &amp;amp; Sorting Using Data from the Database @ Tuesday, March 14, 2006 6:20 PM

Part 2 of the LINQ series shows show to filter and sort using data from the database.


Christian Nagel's OneNotes

# LINQ Part 3 - Filtering and Sorting XML @ Friday, March 17, 2006 5:17 PM

LINQ part 3 shows filtering and sorting XML data using XLINQ and XSLT.
Christian Nagel's OneNotes

# LINQ Part 4 - What's behind a query expression? @ Friday, March 31, 2006 12:24 AM

LINQ part 4 shows what's behind query and lambda expressions.
Christian Nagel's OneNotes

# Language-Integrated Query (LINQ) @ Sunday, April 09, 2006 3:34 AM

I ran across four very interesting links (no pun intended) related to&amp;nbsp;Language-Integrated Query&amp;nbsp;(LINQ)...
Michael Primeaux's Blog

# LINQ vs. XQuery @ Tuesday, April 18, 2006 10:19 AM

What's the difference between XQuery and LINQ?
Christian Nagel's OneNotes

# More LINQ @ Monday, May 15, 2006 10:01 AM

Scott Guthrie has some great information on using LINQ with ASP.NET.
Christian Nagel's OneNotes


Powered by Community Server, by Telligent Systems