I thought of brushing up my LINQ knowledge. I went through MSDN and scribbled some points for my reference. I'm publishing it here.
Language Integrated Query
Before jumping into LINQ, first we need to understand the two programming paradigms .
- Imperative programming
From wiki: In computer science, imperative programming is a programming paradigm that describes computation in terms of statements that change a program state. In much the same way as the imperative mood in natural languages expresses commands to take action; imperative programs define sequences of commands for the computer to perform.
http://en.wikipedia.org/wiki/Imperative_programming
- Functional programming
From wiki: In computer science, functional programming is a programming paradigm that treats computation as the evaluation of mathematical functions and avoids state and mutable data. It emphasizes the application of functions, in contrast to the imperative programming style, which emphasizes changes in state.
http://en.wikipedia.org/wiki/Functional_programming
Traditionally C# programmers use imperative programming practice. Consider we have an employeeList which holds employee information. If we want to find employees who earn salary more than 10k, a C# programmer writes code like
foreach(Employee employee in employeeList){
if(employee.Salary>10000){
newList.Add(employee);
}
}
Consider the employee details are inside Employee table on SQL database. Then we use functional programming to query against the database.
Example: Select * from Employee where Salary>10000 (Please note, select * is not a good practice, this is just for example).
LINQ brings functional programming into the imperative language. That is LINQ contains a set of features that extends powerful query capabilities to the language (C#, Vb.Net) thus unify the way that we work with objects, databases, XML.
To perform a LINQ operation
- Identify the data source (SQL databases, ADO.NET Datasets, XML documents and streams, and .NET collections). We execute LINQ query against this data source
- Define the query expression
- Execute the Query
LINQ Query
A linq query specifies what information to retrieve from data source. A LINQ query is stored in a variable and is initialized by a query expression. As an example
var queryVar = from employee in employeeList where employee.Salary>10000 select employee
In the above example, we declared a query variable queryVar and initialized it with a query expression. The query expression looks like SQL query, but a slightly modified. Here, first we specified the data source (from), then we specified the filter (where) and then select clause specifies the returned elements.
In LINQ, the query variable stores query expression. It won’t do any action or return data. So, the question is when the query variable gets executed. The execution pattern is called deferred execution pattern. So, the actual execution happens when we iterate over the query variable. As an example
foreach(Employee in queryVar){
}
We can force immediate execution of query variable
- Call the aggregation function on query variable (Average, count, max..)
- Call the ToList(Of TSource) or ToArray(Of TSource) methods.Please note, this will cache the execution results.
- Putting the foreach loop immediately after the query expression.
No comments:
Post a Comment