Summary

Section 9.1 Introduction

  • .NET’s collection classes provide reusable data structures that are reliable, powerful and efficient.

  • Lists automatically increase their size to accommodate additional elements.

  • Large amounts of data are often stored in a database—an organized collection of data. Today’s most popular database systems are relational databases. SQL is the international standard language used almost universally with relational databases to perform queries (i.e., to request information that satisfies given criteria).

  • LINQ allows you to write query expressions (similar to SQL queries) that retrieve information from a wide variety of data sources. You can query arrays and Lists, selecting elements that satisfy a set of conditions—this is known as filtering.

  • A LINQ provider is a set of classes that implement LINQ operations and enable programs to interact with data sources to perform tasks such as sorting, grouping and filtering elements.

Section 9.2 Querying an Array of int Values Using LINQ

  • Iteration statements focus on the process of iterating through elements. LINQ specifies the conditions that selected elements must satisfy, not the steps necessary to get the results.

  • The System.Linq namespace contains the classes for LINQ to Objects.

Section 9.2.1 The from Clause

  • A from clause specifies a range variable and the data source to query. The range variable represents each item in the data source (one at a time).

Section 9.2.2 The where Clause

  • If the condition in the where clause evaluates to true for an element, it’s included in the results.

Section 9.2.3 The select Clause

  • The select clause specifies and expression that determines what value appears in the results.

Section 9.2.4 Iterating Through the Results of the LINQ Query

  • A foreach statement can iterate over any object that implements the IEnumerable<T>.

Section 9.2.5 The orderby Clause

  • The orderby clause sorts query results, based on one or more expressions, in ascending order by default. Results also can be sorted in descending order using the descending modifier.

Section 9.2.6 Interface IEnumerable<T>

  • Most LINQ queries return an object that implements the IEnumerable<T> interface.

  • A C# interface describes members that can be used to interact with an object.

  • The IEnumerable<T> interface describes the functionality of any object that’s capable of being iterated over and thus offers methods to access each element in sequence.

  • A class that implements an interface must define each member in the interface.

  • Arrays and generic collections implement the IEnumerable<T> interface.

Section 9.3 Querying an Array of Employee Objects Using LINQ

  • LINQ can be used with collections of any data type.

Section 9.3.2 Sorting a LINQ Query’s Results by Multiple Properties

  • An orderby clause can sort the results according to multiple expressions specified in a comma-separated list.

Section 9.3.3 Any, First and Count Extension Methods

  • Method Any returns true if there’s at least one element in the query to which it’s applied; otherwise, it returns false.

  • The First method returns the first element of the query to which it’s applied. You should check that the query result is not empty before calling First.

  • The Count method returns the number of elements in the query to which it’s applied.

Section 9.3.4 Selecting a Property of an Object

  • The Distinct method removes duplicate values from query results.

Section 9.3.5 Creating New Types in the select Clause of a LINQ Query

  • In a select clause, you can select any number of properties (a projection) and encapsulate them in objects by specifying them in a comma-separated list in braces after the new keyword. The compiler automatically creates a new class having these properties—called an anonymous type.

Section 9.4 Introduction to Collections

  • The .NET collection classes provide efficient methods that organize, store and retrieve data without requiring knowledge of how the data is being stored.

Section 9.4.1 List<T> Collection

  • Class List<T> is similar to an array but provides richer functionality, such as dynamic resizing.

  • The Add method appends an element to the end of a List.

  • The Insert method inserts a new element at a specified position in the List.

  • The Count property returns the number of elements currently in a List.

  • Lists can be indexed like arrays by placing the index in square brackets after the object’s name.

  • The Remove method is used to remove the first element with a specific value.

  • The RemoveAt method removes the element at the specified index.

  • The Contains method returns true if the element is found in the List, and false otherwise.

  • The Capacity property indicates how many items a List can hold without growing.

Section 9.5 Querying the Generic List Collection Using LINQ

  • LINQ to Objects can query Lists.

Section 9.5.1 The let Clause

  • LINQ’s let clause creates a new range variable. This is useful if you need to store a temporary result for use later in the LINQ query.

  • The StartsWith method of the string class determines whether a string starts with the string passed to it as an argument.

Section 9.5.2 Deferred Execution

  • A LINQ query uses deferred execution—it executes only when you access the results, not when you create the query.

Section 9.5.3 Extension Methods ToArray and ToList

  • There may be times when you want to retrieve a collection of the results immediately. LINQ provides extension methods ToArray and ToList for this purpose.

Section 9.5.4 Collection Initializers

  • Collection initializers provide a convenient syntax (similar to array initializers) for initializing a collection.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset