LINQ -Language Integrated Query
LINQ is set of extensions to the .Net Framework 3.5 and its managed languages that sets the query as an object. It defines a common syntax and a programming model to query different types of data using a common language.
The relational operators like Select, Project, Join, Group, Partition, Set operations etc., are implemented in LINQ and the C# and VB compilers in the .Net framework 3.5, which support the LINQ syntax makes it possible to work with a configured data store without resorting to ADO.Net.
For example querying the Customers table in the Northwind database, using LINQ query in C#, the code would be:
var data = from c in dataContext.Customers
where c.Name == "Rahul"
The ‘from’ keyword logically loops through the contents of the collection.
The expression with the ‘where‘ keyword is evaluated for each object in the collection.
The ‘select‘ statement selects the evaluated object to add to the list being returned.
The ‘var’ keyword is for variable declaration. Since the exact type of the returned object is not known, it indicates that the information will be inferred dynamically.
LINQ query can be applied to any data-bearing class that inherits from IEnumerable, here T is any data type.
Apart from the operators used so far, there are several other operators, which implement all query clauses. Let us look at some of the operators and clauses.
The Join clause:
The ‘join clause’ in SQL is used for joining two data tables and displays a data set containing columns from both the tables. LINQ is also capable of that.
The Where clause:
The ‘where clause’ allows adding some conditional filters to the query.
The Orderby and Orderbydescending clauses:
These clauses allow sorting the query results.
The Let clause:
The let clause allows defining a variable and assigning it a value calculated from the data values.