Free mag vol1 | Page 505

CHAPTER 12  LINQ TO OBJECTS Table 12-2. Core LINQ-Centric Assemblies Assembly Meaning in Life System.Core.dll Defines the types that represent the core LINQ API. This is the one assembly you must have access to if you want to use any LINQ API, including LINQ to Objects. System.Data.DataSetExtensions.dll Defines a handful of types to integrate ADO.NET types into the LINQ programming paradigm (LINQ to DataSet). System.Xml.Linq.dll Provides functionality for using LINQ with XML document data (LINQ to XML). In order to work with LINQ to Objects, you must make sure that every C# code file that contains LINQ queries imports the System.Linq namespace (primarily defined within System.Core.dll). If you do not do so, you will run into a number of problems. As a very good rule of thumb, if you see a compiler error looking similar to this: Error 1 Could not find an implementation of the query pattern for source type 'int[]'. 'Where' not found. Are you missing a reference to 'System.Core.dll' or a using directive for 'System.Linq'? the chances are extremely good that your C# file does not have the following using directive (and believe me, I speak from experience!): using System.Linq; Applying LINQ Queries to Primitive Arrays To begin examining LINQ to Objects, let’s build an application that will apply LINQ queries to various array objects. Create a Console Application named LinqOverArray, and define a static helper method within the Program class named QueryOverStrings(). In this method, create a string array containing six or so items of your liking (here I listed out a batch of video games I am currently attempting to finish). Make sure to have at least two entries that contain numerical values, and a few that have embedded spaces. static void QueryOverStrings() { // Assume we have an array of strings. string[] currentVideoGames = {"Morrowind", "Uncharted 2", "Fallout 3", "Daxter", "System Shock 2"}; } Now, update Main() to invoke QueryOverStrings(): 445