LINQ (Language Integrated Query) is uniform query syntax to retrieve data from different sources and formats supported in C# and VB.Net. LINQ helps us to retrieve and filter data in a convenient and uniform way from different sources like SQL databases, ADO .NET Datasets, XML documents and .NET collections.
Namespace : System.Linq
// LINQ Example
var vehicles = new List<string>()
"Car - BMW",
"Car - Ford"
// LINQ Query
var result = from v in vehicles
//iterate through the result collection
// query is executed here
foreach (var vehicle in result)
In the above example, from the vehicles collection, vehicle list containing string “Car” is selected. However, it is to be noted that query is executed when the query variable is iterated, not when the query is assigned to the query variable. This is known as deferred execution of LINQ query.
var result = (from v in vehicles
In above query, query is forced to execute immediately as the result is taken as List by using the ToList() exetnesion method. This is known as immediate execution.