Free mag vol1 | Page 529

CHAPTER 12  LINQ TO OBJECTS // Build the necessary Func<> delegates using anonymous methods. Func searchFilter = delegate(string game) { return game.Contains(" "); }; Func itemToProcess = delegate(string s) { return s; }; // Pass the delegates into the methods of Enumerable. var subset = currentVideoGames.Where(searchFilter) .OrderBy(itemToProcess).Select(itemToProcess); // Print out the results. foreach (var game in subset) Console.WriteLine("Item: {0}", game); Console.WriteLine(); } This iteration of the query expression is even more verbose, because you are manually creating the Func<> delegates used by the Where(), OrderBy(), and Select() methods of the Enumerable class. On the plus side, the anonymous method syntax does keep all the delegate processing contained within a single method definition. Nevertheless, this method is functionally equivalent to the QueryStringsWithEnumerableAndLambdas() and QueryStringsWithOperators() methods created in the previous sections. Building Query Expressions Using the Enumerable Type and Raw Delegates Finally, if you want to build a query expression using the really verbose approach, you could avoid the use of lambdas/anonymous method syntax and directly create delegate targets for each Func<> type. Here is the final iteration of your query expression, modeled within a new class type named VeryComplexQueryExpression: class VeryComplexQueryExpression { public static void QueryStringsWithRawDelegates() { Console.WriteLine("***** Using Raw Delegates *****"); string[] currentVideoGames = {"Morrowind", "Uncharted 2", "Fallout 3", "Daxter", "System Shock 2"}; // Build the necessary Func<> delegates. Func searchFilter = new Func(Filter); Func itemToProcess = new Func(ProcessItem); // Pass the delegates into the methods of Enumerable. var subset = currentVideoGames .Where(searchFilter).OrderBy(itemToProcess).Select(itemToProcess); // Print out the results. foreach (var game in subset) Console.WriteLine("Item: {0}", game); Console.WriteLine(); 469