LINQ

suggest change

Versions

[{“Name”:“3.5”,“GroupName”:”.NET”},{“Name”:“3.5”,“GroupName”:“Compact Framework”},{“Name”:“3.5 SP1”,“GroupName”:”.NET”},{“Name”:“3.7”,“GroupName”:“Compact Framework”},{“Name”:“4.0”,“GroupName”:”.NET”},{“Name”:“4.5”,“GroupName”:”.NET”},{“Name”:“3.9”,“GroupName”:“Compact Framework”},{“Name”:“4.5.1”,“GroupName”:”.NET”},{“Name”:“4.5.2”,“GroupName”:”.NET”},{“Name”:“4.6”,“GroupName”:”.NET”},{“Name”:“4.6.1”,“GroupName”:”.NET”},{“Name”:“4.6.2”,“GroupName”:”.NET”}]

Introduction

LINQ (Language Integrated Query) is an expression that retrieves data from a data source. LINQ simplifies this situation by offering a consistent model for working with data across various kinds of data sources and formats. In a LINQ query, you are always working with objects. You use the same basic coding patterns to query and transform data in XML documents, SQL databases, ADO.NET Datasets, .NET collections, and any other format for which a provider is available. LINQ can be used in C# and VB.

Syntax

Remarks

The LINQ built-in methods are extension methods for the IEnumerable<T> interface that live in the System.Linq.Enumerable class in the System.Core assembly. They are available in .NET Framework 3.5 and later.

LINQ allows for simple modification, transformation, and combination of various IEnumerables using a query-like or functional syntax.

While the standard LINQ methods can work on any IEnumerable<T>, including the simple arrays and List<T>s, they can also be used on database objects, where the set of LINQ expressions can be transformed in many cases to SQL if the data object supports it. See LINQ to SQL.

For the methods that compare objects (such as Contains and Except), IEquatable<T>.Equals is used if the type T of the collection implements that interface. Otherwise, the standard Equals and GetHashCode of the type (possibly overriden from the default Object implementations) are used. There are also overloads for these methods that allow to specify a custom IEqualityComparer<T>.

For the ...OrDefault methods, default(T) is used to generate default values.

Official reference: Enumerable class

Lazy Evaluation

Virtually every query that returns an IEnumerable<T> is not evaluated immediately; instead, the logic is delayed until the query is iterated over. One implication is that each time someone iterates over an IEnumerable<T> created from one of these queries, e.g., .Where(), the full query logic is repeated. If the predicate is long-running, this can be a cause for performance issues.

One simple solution (when you know or can control the approximate size of the resulting sequence) is to fully buffer the results using .ToArray() or .ToList(). .ToDictionary() or .ToLookup() can fulfill the same role. One can also, of course, iterate over the entire sequence and buffer the elements according to other custom logic.

ToArray() or ToList()?

Both .ToArray() and .ToList() loop through all elements of an IEnumerable<T> sequence and save the results in a collection stored in-memory. Use the following guidelines to determine which to choose:

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents