WORKING WITH LINQ TO XML in C#

Print ANSI/AIM Code 39 in C# WORKING WITH LINQ TO XML

CHAPTER 13 WORKING WITH LINQ TO XML
Generate Code 3/9 In C#.NET
Using Barcode creator for VS .NET Control to generate, create ANSI/AIM Code 39 image in VS .NET applications.
www.OnBarcode.com
Scan Code-39 In C#.NET
Using Barcode decoder for Visual Studio .NET Control to read, scan read, scan image in Visual Studio .NET applications.
www.OnBarcode.com
the queries is often unknown at the time of writing the query. To indicate such an anonymous type, C# uses the var keyword. The LINQ query that follows indicates that you want to fetch all the items from the employees collection and want to group them by Country values. At run time, this LINQ query returns a dictionary. The outer foreach loop iterates through all the items of this dictionary. With each iteration, the key of the dictionary item is outputted in the TextBox using the OutputResults() helper method (discussed next). The inner foreach loop iterates through all the employees from a group and emits their details in the TextBox. Listing 13-3 shows the code only for the Country field, but the other fields will follow the same structure. The OutputResults() helper method has two overloads as shown in Listing 13-4. Listing 13-4. Outputting Results to a TextBox private void OutputResults(Employee emp) { txtResults.Text += "[" + emp.EmployeeID + "] "; txtResults.Text += emp.FirstName + " " + emp.LastName + "(" + emp.BirthDate.ToShortDateString() + ")," + emp.Country + "\r\n"; } private void OutputResults(string msg) { txtResults.Text += msg + "\r\n"; } The first overload of the OutputResults() helper method accepts an Employee object and appends its property values to the TextBox contents. The second overload simply accepts a string value and appends it to the TextBox contents.
UCC-128 Maker In Visual C#.NET
Using Barcode drawer for .NET framework Control to generate, create UCC-128 image in .NET framework applications.
www.OnBarcode.com
ECC200 Drawer In C#
Using Barcode creator for .NET Control to generate, create Data Matrix ECC200 image in VS .NET applications.
www.OnBarcode.com
Sorting Data Using LINQ
ANSI/AIM Code 39 Printer In Visual C#
Using Barcode creator for Visual Studio .NET Control to generate, create Code 39 Extended image in .NET applications.
www.OnBarcode.com
Drawing Linear Barcode In Visual C#
Using Barcode drawer for Visual Studio .NET Control to generate, create Linear image in .NET framework applications.
www.OnBarcode.com
Now let s implement the sorting functionality. The sorting operation is done in the Click event handler of the second Show button. The part of the event handler is shown in Listing 13-5. Listing 13-5. Sorting Data Using LINQ private void button2_Click(object sender, EventArgs e) { txtResults.Clear(); if (comboBox2.SelectedItem.ToString() == "Country") { var result = from employee in employees orderby employee.Country select employee;
Encoding Matrix In Visual C#
Using Barcode printer for .NET Control to generate, create 2D image in .NET framework applications.
www.OnBarcode.com
Painting Identcode In Visual C#.NET
Using Barcode creator for .NET framework Control to generate, create Identcode image in Visual Studio .NET applications.
www.OnBarcode.com
C HA P TER 13 WO RK ING W IT H LI NQ T O XM L
Encoding Code 3/9 In VS .NET
Using Barcode creation for Reporting Service Control to generate, create Code 3 of 9 image in Reporting Service applications.
www.OnBarcode.com
Code 39 Full ASCII Scanner In Java
Using Barcode scanner for Java Control to read, scan read, scan image in Java applications.
www.OnBarcode.com
foreach (var employee in result) { OutputResults(employee); } } ... } The orderby clause of a LINQ query sorts the results based on the indicated fields. The code in Listing 13-5 sorts the data on the basis of Country values. Notice the use of the select keyword that decides what to fetch. We will revisit the select keyword in later sections. The foreach loop then iterates the results and emits them in the TextBox.
Create EAN 13 In Java
Using Barcode maker for Java Control to generate, create European Article Number 13 image in Java applications.
www.OnBarcode.com
Reading Code 3 Of 9 In .NET
Using Barcode reader for Visual Studio .NET Control to read, scan read, scan image in VS .NET applications.
www.OnBarcode.com
Filtering Data Using LINQ
Making Code 3/9 In Objective-C
Using Barcode creator for iPad Control to generate, create Code 39 Full ASCII image in iPad applications.
www.OnBarcode.com
Recognize DataMatrix In None
Using Barcode scanner for Software Control to read, scan read, scan image in Software applications.
www.OnBarcode.com
The employees list contains details about all the employees in the system. Often you need to work with a subset of the total data based on some criteria. The where clause of LINQ queries allows you to filter the data based on some condition. In our example, the Click event handler of the third Show button filters the employees list based on the criteria entered in the corresponding TextBox. A part of the relevant code is shown in Listing 13-6. Listing 13-6. Filtering Data Using LINQ private void button3_Click(object sender, EventArgs e) { txtResults.Clear(); if (comboBox3.SelectedItem.ToString() == "Country") { var result = from employee in employees where employee.Country==textBox1.Text select employee; foreach (var employee in result) { OutputResults(employee); } } ... } The code from Listing 13-6 filters the employees list for a specific country as supplied in the TextBox. Our example uses the == operator of C#. You can also use the && (AND) and || (OR) operators to create more complex conditions. The results are then outputted to the TextBox as before.
Code 128 Code Set A Scanner In None
Using Barcode recognizer for Software Control to read, scan read, scan image in Software applications.
www.OnBarcode.com
Universal Product Code Version A Encoder In None
Using Barcode encoder for Microsoft Excel Control to generate, create UPC-A image in Office Excel applications.
www.OnBarcode.com
Selecting Data Using LINQ
Generate Data Matrix 2d Barcode In None
Using Barcode drawer for Word Control to generate, create ECC200 image in Microsoft Word applications.
www.OnBarcode.com
Code 39 Full ASCII Encoder In None
Using Barcode creator for Font Control to generate, create Code 39 Full ASCII image in Font applications.
www.OnBarcode.com
In all the LINQ queries so far, we fetched the entire Employee instance. What if we want only a part of the Employee instance or some computed data That is where the select clause comes in handy. The select clause can be used to control the shape of the result (also known as the
PDF417 Drawer In Java
Using Barcode printer for Java Control to generate, create PDF-417 2d barcode image in Java applications.
www.OnBarcode.com
Make EAN-13 In Visual Studio .NET
Using Barcode printer for .NET framework Control to generate, create UPC - 13 image in VS .NET applications.
www.OnBarcode.com
CHAPTER 13 WORKING WITH LINQ TO XML
projection). In our examples, the output field s combo box governs which of the fields will be returned in the output. The actual shape of the output is decided by LINQ queries that go inside the Click event of the fourth Show button (see Listing 13-7). Listing 13-6. Selecting Data Using LINQ private void button4_Click(object sender, EventArgs e) { txtResults.Clear(); if(comboBox4.SelectedIndex==0) { var result = from employee in employees select new { employee.EmployeeID, employee.FirstName, employee.LastName }; foreach (var emp in result) { OutputResults("[" + emp.EmployeeID + "] " + emp.FirstName + " " + emp.LastName); } } ... } The code first decides which of the fields of the Employee object are to be selected. It then constructs a new anonymous object that includes EmployeeID, FirstName, and LastName properties. The results are then outputted in the TextBox. You can now run the application and test how our LINQ queries work.
Copyright © OnBarcode.com . All rights reserved.