Home Random Page


CATEGORIES:

BiologyChemistryConstructionCultureEcologyEconomyElectronicsFinanceGeographyHistoryInformaticsLawMathematicsMechanicsMedicineOtherPedagogyPhilosophyPhysicsPolicyPsychologySociologySportTourism






Task 1 – Querying using the Standard Query Operators

1. The query expression syntax shown in previous examples (expressions starting with “from”) is not the only method of querying data using LINQ. LINQ introduces various methods known as query operators that form the basis of the query expression functionality you’ve seen, which you can also access directly in a more familiar method-based way. Create a new method for this exercise:

 

static void OperatorQuery()

{

var db = new NorthwindDataContext();

 

var matchingCustomers = db.Customers

.Where(c => c.City.Contains("London"));

 

foreach (var c in matchingCustomers)

Console.WriteLine("{0}\t{1}\t{2}",

c.CustomerID, c.ContactName, c.City);

}

 

static void ObjectQuery()

{

The parameter passed to the Where method is known as a lambda expression, another feature introduced in C# 3.0. Lambda expressions are shorthand for defining a function in-line, similar to anonymous methods in C#2.0. The first token "c" represents the current element of the sequence, in this case the Customers table. The => token means substitute c in the expression body, and the c.City.Contains("London") expression acts as the where clause. In LINQ to SQL this expression is emitted as an expression tree, which LINQ to SQL uses to construct the equivalent SQL statement at runtime.

 

2. Call this method from Main:

static void Main(string[] args)

{

OperatorQuery();

}

 

3. Press Ctrl+F5 to build and run the application. Then press any key to terminate the application.

 

Notice that little has changed in this example. The query is now calling the standard query operators directly, but the result is the same as previous queries that selected only those customers that live in London.

 

4. Data aggregation can be obtained by simply calling the standard query operators on the result, just as you would with any other method. Replace the code in OperatorQuery to determine the average unit price of all products starting with the letter "A":

 

static void OperatorQuery()

{

var db = new NorthwindDataContext();

 

var avgCost = db.Products

.Where(p => p.ProductName.StartsWith("A"))

.Select(p => p.UnitPrice)

.Average();

 

Console.WriteLine("Average cost = {0:c}", avgCost);

}

 

5. Press Ctrl+F5 to build and run the application. View the results and afterward press any key to terminate the application.

 

 

The result now shows the average cost for products whose names start with "A". From left to right, first the table (db.Products) is specified and then the results are restricted to those rows with product names beginning with an "A". To this first filtered set of results two more operators are applied. Then the UnitPrice column is selected, getting back a collection of prices, one for each original result. Finally, using the Average operator the collection of prices are averaged and returned as a single value.




Date: 2015-02-03; view: 850


<== previous page | next page ==>
Task 8 – Expanding Query Expressions | Task 2 – Working with the Select Operator
doclecture.net - lectures - 2014-2024 year. Copyright infringement or personal data (0.007 sec.)