.NET’s collection classes provide reusable data structures that are reliable, powerful and efficient.
List
s 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 List
s, 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.
int
Values Using LINQIteration 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.
from
ClauseA 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).
where
ClauseIf the condition in the where
clause evaluates to true
for an element, it’s included in the results.
select
ClauseThe select
clause specifies and expression that determines what value appears in the results.
A foreach
statement can iterate over any object that implements the IEnumerable<T>
.
orderby
ClauseThe 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.
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.
Employee
Objects Using LINQLINQ can be used with collections of any data type.
An orderby
clause can sort the results according to multiple expressions specified in a comma-separated list.
Any
, First
and Count
Extension MethodsMethod 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.
The Distinct
method removes duplicate values from query results.
select
Clause of a LINQ QueryIn 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.
The .NET collection classes provide efficient methods that organize, store and retrieve data without requiring knowledge of how the data is being stored.
List<T>
CollectionClass 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
.
List
s 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.
List
Collection Using LINQLINQ to Objects can query List
s.
let
ClauseLINQ’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.
A LINQ query uses deferred execution—it executes only when you access the results, not when you create the query.
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.
Collection initializers provide a convenient syntax (similar to array initializers) for initializing a collection.