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.
6f21ca17-0c1a-4588-8cf0-fbac310151bf|0|.0