- Home
- Products
- Integration
- Tutorial
- Barcode FAQ
- Purchase
- Company
LINQ TO OBJECTS in Visual C#.NET
CHAPTER 27 LINQ TO OBJECTS Data Matrix Creation In Visual C#.NET Using Barcode maker for Visual Studio .NET Control to generate, create DataMatrix image in VS .NET applications. www.OnBarcode.comScan Data Matrix In Visual C#.NET Using Barcode reader for .NET Control to read, scan read, scan image in .NET applications. www.OnBarcode.comListing 27-9. Creating a More Complex Condition in a where Clause using System; using System.Collections.Generic; using System.Linq; class Listing 09 { static void Main(string[] args) { List<string> myFruitList = new List<string>() { "apple", "plum", "cherry", "grape", "banana", "pear", "mango" , "persimmon", "lemon", "lime", "coconut", "pineapple", "orange"}; IEnumerable<string> results = from e in myFruitList where e[0] == 'p' && e[1] == 'e' && e.Length > 4 select e; foreach (string str in results) { Console.WriteLine("Result item: {0}", str); } // wait for input before exiting Console.WriteLine("Press enter to finish"); Console.ReadLine(); } } The query in Listing 27-9 filters the data elements, allowing only those whose first letter is p, whose second letter is e, and whose Length is greater than four characters. Compiling and running Listing 27-9 produces the following results: Result item: persimmon Press enter to finish UPC Symbol Generation In Visual C#.NET Using Barcode maker for Visual Studio .NET Control to generate, create UPC-A image in VS .NET applications. www.OnBarcode.comPainting Barcode In C# Using Barcode creator for .NET framework Control to generate, create Barcode image in Visual Studio .NET applications. www.OnBarcode.comProjecting Data
Barcode Printer In C# Using Barcode generator for VS .NET Control to generate, create Barcode image in VS .NET applications. www.OnBarcode.comEncode GTIN - 128 In C# Using Barcode printer for Visual Studio .NET Control to generate, create EAN128 image in VS .NET applications. www.OnBarcode.comProjecting data, also known as selecting data, is performed by the select clause of a LINQ query. When you project data, you select what will be placed into the results for each item that is being processed. The projected data can be the same as the source data, values from one or more members of the source data, or different data entirely. You have already seen how to project the same data as in the results (Listing 27-9, for example). Almost all LINQ queries end with a select clause. In the following sections, I ll show you how to do some other kinds of projection. EAN / UCC - 13 Encoder In C#.NET Using Barcode drawer for VS .NET Control to generate, create EAN 13 image in .NET applications. www.OnBarcode.comISBN - 10 Maker In C#.NET Using Barcode maker for .NET Control to generate, create International Standard Book Number image in Visual Studio .NET applications. www.OnBarcode.comCHAPTER 27 LINQ TO OBJECTS
DataMatrix Reader In VB.NET Using Barcode recognizer for VS .NET Control to read, scan read, scan image in .NET applications. www.OnBarcode.comData Matrix Printer In VS .NET Using Barcode creator for ASP.NET Control to generate, create Data Matrix 2d barcode image in ASP.NET applications. www.OnBarcode.comProjecting a Single Member
Barcode Generation In Objective-C Using Barcode drawer for iPhone Control to generate, create Barcode image in iPhone applications. www.OnBarcode.comRecognize Code 3 Of 9 In VB.NET Using Barcode scanner for .NET framework Control to read, scan read, scan image in VS .NET applications. www.OnBarcode.comYou can project the value of a single member of a data type into the results by selecting that value in the select clause of your LINQ query. We already saw this when we selected the first character of a string value using an indexer, but Listing 27-10 provides another example. Listing 27-10. Projecting a Single Member using System; using System.Collections.Generic; using System.Linq; class Listing 10 { static void Main(string[] args) { List<string> myFruitList = new List<string>() { "apple", "plum", "cherry", "grape", "banana", "pear", "mango" , "persimmon", "lemon", "lime", "coconut", "pineapple", "orange"}; IEnumerable<int> results = from e in myFruitList where e[0] == 'p' select e.Length; foreach (int i in results) { Console.WriteLine("Result item: {0}", i); } // wait for input before exiting Console.WriteLine("Press enter to finish"); Console.ReadLine(); } } The generic type of the data source is string in Listing 27-10, and the select clause in projects the value of the Length member, which is an int. Notice that the generic type of the results has changed to int as well. The result from this query will be a sequence of int values representing the number of characters in the fruit names. The LINQ query in Listing 27-10 has a where clause as well. I have included this to demonstrate that you can combine the various LINQ operations to create more complex queries. In this query, the where clause filters the data items so only those that start with the letter p are passed to the select clause, which then projects the Length value. Compiling and running Listing 27-10 produces the following results: Result item: 4 Result item: 4 Result item: 9 Result item: 9 Press enter to finish ECC200 Creator In Visual Studio .NET Using Barcode encoder for VS .NET Control to generate, create Data Matrix image in .NET applications. www.OnBarcode.comDecoding Barcode In Java Using Barcode decoder for Java Control to read, scan read, scan image in Java applications. www.OnBarcode.comCHAPTER 27 LINQ TO OBJECTS
EAN / UCC - 13 Recognizer In Visual C#.NET Using Barcode reader for Visual Studio .NET Control to read, scan read, scan image in VS .NET applications. www.OnBarcode.comCreate Barcode In Java Using Barcode drawer for Eclipse BIRT Control to generate, create Barcode image in BIRT reports applications. www.OnBarcode.comProjecting Anonymous Types
QR Code 2d Barcode Creation In Java Using Barcode creator for BIRT Control to generate, create QR image in Eclipse BIRT applications. www.OnBarcode.comCreate UCC - 12 In None Using Barcode drawer for Font Control to generate, create USS-128 image in Font applications. www.OnBarcode.comYou can select more than one member from a data type by projecting an anonymous type (anonymous types are explained in 15). Listing 27-11 provides an example. Listing 27-11. Projecting an Anonymous Type using System; using System.Collections.Generic; using System.Linq; class Listing 11 { static void Main(string[] args) { List<string> myFruitList = new List<string>() { "apple", "plum", "cherry", "grape", "banana", "pear", "mango" , "persimmon", "lemon", "lime", "coconut", "pineapple", "orange"}; var results = from e in myFruitList select new { Length = e.Length, FirstChar = e[0] }; foreach (var item in results) { Console.WriteLine("Result item - Length: {0}, FirstChar: {1}", item.Length, item.FirstChar); } // wait for input before exiting Console.WriteLine("Press enter to finish"); Console.ReadLine(); } } The anonymous type in Listing 27-11 contains two properties. The Length property contains the number of characters in the fruit name, and the FirstChar property contains the first character in the name. You must use the var keyword for the result generic type when you project an anonymous type and when you enumerate the results, as shown in the listing. Compiling and running Listing 27-11 produces the following results: Result Result Result Result Result Result Result item item item item item item item Length: Length: Length: Length: Length: Length: Length: 5, 4, 6, 5, 6, 4, 5, FirstChar: FirstChar: FirstChar: FirstChar: FirstChar: FirstChar: FirstChar: a p c g b p m EAN-13 Supplement 5 Generation In Java Using Barcode creator for Java Control to generate, create EAN13 image in Java applications. www.OnBarcode.comBarcode Creator In None Using Barcode maker for Online Control to generate, create Barcode image in Online applications. www.OnBarcode.com |
|