Home Random Page


CATEGORIES:

BiologyChemistryConstructionCultureEcologyEconomyElectronicsFinanceGeographyHistoryInformaticsLawMathematicsMechanicsMedicineOtherPedagogyPhilosophyPhysicsPolicyPsychologySociologySportTourism






Task 8 – Expanding Query Expressions

 

1. So far, the queries demonstrated in this lab have been primarily based on filtering. However, LINQ supports many options for querying data that go beyond simple filtering. For example, to sort customers in London by ContactName, you can use the orderby clause (also be sure to set the Main method to call ObjectQuery again):

 

static void ObjectQuery()

{

var db = new NorthwindDataContext();

db.Log = Console.Out;

var results = from c in db.Customers

where c.City == "London"

orderby c.ContactName descending

select new { c.ContactName, c.CustomerID };

foreach (var c in results)

Console.WriteLine("{0}\t{1}", c.CustomerID, c.ContactName);

}

 

static void Main(string[] args)

{

ObjectQuery();

}

 

2. Press Ctrl+F5 to build and run the application. Notice the customers are sorted by the second column, the name column, in a descending manner. Press any key to terminate the application.

3. Continue with different types of queries: write a query that finds the number of customers located in each city. To do this make use of the group by expression.

 

static void ObjectQuery()

{

var db = new NorthwindDataContext();

db.Log = Console.Out;

var results = from c in db.Customers

group c by c.City into g

orderby g.Count() ascending

select new { City = g.Key, Count = g.Count() };

foreach (var c in results)

Console.WriteLine("{0}\t{1}", c.City, c.Count);

}

 

4. Press Ctrl+F5 to run the application. After viewing the results , press any key to terminate the application.

5. Often when writing queries you want to search through two tables. This is usually performed using a join operation, which is supported in C# 3.0. In ObjectQuery replace the previous query with this one. Recall your query printed out all orders for each customer that lives in London. This time, instead of printing all the orders, print the number of orders per customer.

 

static void ObjectQuery()

{

var db = new NorthwindDataContext();

db.Log = Console.Out;

var results = from c in db.Customers

join e in db.Employees on c.City equals e.City

group e by e.City into g

select new { City = g.Key, Count = g.Count() };

 

foreach (var c in results)

Console.WriteLine("{0}\t{1}", c.City, c.Count);

}

 

6. Press Ctrl+F5 to run the application. Taking a look at the output, the SQL query generated can also be seen. Press any key to terminate the application.

 

This example illustrates how a SQL style join can be used when there is no explicit relationship to navigate.

Exercise 5 – Understanding the Standard Query Operators [Optional]

LINQ provides more than forty different query operators, of which only a small sample are highlighted here. Additional operators can also be added programmatically through the standard query operator APIs.

In this exercise, you learn about several of the query operators available for data access and manipulation. These operators are a set of methods that every LINQ provider should implement.



 


Date: 2015-02-03; view: 709


<== previous page | next page ==>
Task 6 – Modifying Database Data | Task 1 – Querying using the Standard Query Operators
doclecture.net - lectures - 2014-2024 year. Copyright infringement or personal data (0.007 sec.)