Module5 Chap21 C#
Module5 Chap21 C#
• Explain how LINQ defers evaluation of a query and how you can force
immediate execution and cache the results of a LINQ query.
• Important aspect of the language that is likely to be used by many
applications: the support that C# provides for querying data.
• We have seen that we can define structures and classes for modeling
data and that we can use collections and arrays for temporarily storing
data in memory.
• SQL provides a high-level description of the data that the developer wants to
retrieve but does not indicate exactly how the database management system
should retrieve this data.
• Consequently, an application that invokes SQL statements does not care how the
database management system physically stores or retrieves data.
– The format used by the database management system can change (for
example, if a new version is released) without the application developer needing
to modify the SQL statements used by the application.
• LINQ provides syntax and semantics very reminiscent of SQL, and
with many of the same advantages.
• It is far more flexible and can handle a wider variety of logical data
structures.
• There are three important things that is needed to understand at this point:
• cust is an alias for each row in the customers array. The compiler
deduces this from the fact that we are calling the Select method on
the customers array. Any legal C# identifier can be used in place
of cust.
– The Select method does not actually retrieve the data at this time;
• The preceding example uses the Select method of the customers array to
generate an IEnumerable<string> object named customerFirstNames. (It is
of type IEnumerable<string> because the Select method returns an
enumerable collection of customer first names, which are strings.)
• foreach statement iterates through this collection of strings, printing out the
first name of each customer in the following sequence:
• How to fetch the first and last name of each customer?
– We can concatenate the first and last names together into a single
string in the Select method, like this:
– We can define a new type that wraps the first and last names, and
use the Select method to construct instances of this type, like this:
Filtering Data
• The Select method enables us to specify, or project, the fields that we
want to include in the enumerable collection.
• It expects a parameter that defines a method that filters the data according to
whatever criteria you specify.
• The variable addr is an alias for a row in the addresses array, and the lambda
expression returns all rows where the Country field matches the string “United
States”.
• The Select method is then applied to these rows to project only the
CompanyName field from this enumerable collection to return another
enumerable collection of string objects. (The variable usComp is an alias for
the type of each row in the enumerable collection returned by the Where
method.)
• The type of the result of this complete expression is therefore
IEnumerable<string>.
• The foreach statement that iterates through this collection displays the
following companies:
Ordering, Grouping, and Aggregating Data
• To retrieve data in a particular order, you can use the OrderBy method.
• Like the Select and Where methods, OrderBy expects a method as its
argument.
• This method identifies the expressions that you want to use to sort the
data. For example, you can display the name of each company in the
addresses array in ascending order, like this:
• If you want to enumerate the data in descending order, you can use the
OrderByDescending method instead.
• If you want to order by more than one key value, you can use the ThenBy
or ThenByDescending method after OrderBy or OrderByDescending.
• To group data according to common values in one or more fields, you
can use the GroupBy method. The next example shows how to group the
companies in the addresses array by country:
• We can access the value we are grouping by using the Key field of each
item, and we can also calculate summary data for each group by using
methods such as Count, Max, Min, and many others. The output
generated by the example code looks like this:
• You can use many of the summary methods such as Count, Max, and
Min directly over the results of the Select method.
• If you want to know how many companies there are in the addresses
array, you can use a block of code such as this:
• In fact, there are only three different countries in the addresses array—it
just so happens that United States and United Kingdom both occur twice.
• The from operator defines an alias for the source collection, and the
select operator specifies the fields to retrieve by using this alias.
– If you are familiar with SQL, notice that the from operator occurs
before the select operator.
• To retrieve the first and last names for each customer, you can use the
following statement.
• We use the where operator to filter data. The following example shows
how to return the names of the companies based in the United States
from the addresses array: