Home Random Page


CATEGORIES:

BiologyChemistryConstructionCultureEcologyEconomyElectronicsFinanceGeographyHistoryInformaticsLawMathematicsMechanicsMedicineOtherPedagogyPhilosophyPhysicsPolicyPsychologySociologySportTourism






Task 3 – Querying Structured Types

1. In this task, you move beyond primitive types and apply query features to custom structured types. Above the Program class declaration, add the following code to create a Customer class:

 

public class Customer

{

public string CustomerID { get; set; }

public string City { get; set; }

 

public override string ToString()

{

return CustomerID + "\t" + City;

}

}

class Program

 

Notice the use of another new C# language feature, auto-implemented properties. In the preceding code, this feature creates two properties with automatic backing fields. Also notice that no constructor has been declared. In earlier versions of C#, this would have required consumers to create an instance of the object using the default parameterless constructor and then set the fields explicitly as separate statements.

 

2. Within the Program class declaration, create the following new method, which creates a list of customers (taken from the Northwind database):

static void Main(string[] args)

{

NumQuery();

}

static IEnumerable<Customer> CreateCustomers()

{

return new List<Customer>

{

new Customer { CustomerID = "ALFKI", City = "Berlin" },

new Customer { CustomerID = "BONAP", City = "Marseille" },

new Customer { CustomerID = "CONSH", City = "London" },

new Customer { CustomerID = "EASTC", City = "London" },

new Customer { CustomerID = "FRANS", City = "Torino" },

new Customer { CustomerID = "LONEP", City = "Portland" },

new Customer { CustomerID = "NORTS", City = "London" },

new Customer { CustomerID = "THEBI", City = "Portland" }

};

}

 

There are several interesting things to note about this code block. First of all, notice that the new collection is being populated directly within the curly braces. That is, even though the type is List<T>, not an array, it is possible to use braces to immediately add elements without calling the Add method. Second, notice that the Customer elements are being initialized with a new syntax (known as object initializers).

 

3. Next query the collection for customers that live in London. Add the following query method ObjectQuery and add a call to it within the Main method (removing the call to StringQuery).

static void ObjectQuery()

{

var results = from c in CreateCustomers()

where c.City == "London"

select c;

 

foreach (var c in results)

Console.WriteLine(c);

}

 

static void Main(string[] args)

{

ObjectQuery();

}

 

Notice that again the compiler is using type inference to strongly type the results variable in the foreach loop.

 

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

 

Three results are shown. As you can see, when using LINQ query expressions, working with complex types is just as easy as working with primitive types.



 

 


Date: 2015-02-03; view: 730


<== previous page | next page ==>
Task 2 – Querying a Generic List of Integers | Task 2 – Querying by Reading in an XML File
doclecture.net - lectures - 2014-2024 year. Copyright infringement or personal data (0.007 sec.)