The acronym LINQ stands for Language Integrated Query. LINQ syntax is roughly derived from SQL and was introduced with .NET 3.5
The LINQ paradigm describes a method for programmatically accessing and manipulating any data, independent of the source. LINQ integrates query capabilities directly into the C# language. It provides a consistent query experience for in-memory objects (LINQ to Objects), relational databases (LINQ to SQL), and XML (LINQ to XML).
Getting started with basic LINQ queries
Query 1: From a give array of strings, below query filters the ones with the letter ‘a’ in it
var words =newstring[]{"falcon","eagle","sky","tree","water"};// Query syntaxvar res =from word in wordswhereword.Contains('a')select word;foreach(var word in res)Console.WriteLine(word);Console.ReadKey();
Things to note:
The above LINQ query is written in what is called as query syntax
The above snippet will not compile without using System.Linq; It is the namespace required to use LINQ in .NET framework
We can use this snippet in any kind of .NET project, like Console, WinForms or Web based projects
We have used the var keyword to hold the results of the query, since it is convenient
The actual return type would be IEnumerable<string>
The IEnumerable interface is central to LINQ. We can use LINQ on any object that implements IEnumerable
We can even create our own classes that implements IEnumerable, and those classes will instantly "inherit" all LINQ functionality!
We can re-write the same using what is called method syntax as shown below
We can re-write the same replacing var with IEnumerable as shown below:
In all cases, the o/p will be
Query 2: From a given array of integers, below query filters the even ones in it
In the queries above, since we have used LINQ on in-memory objects, we call this mode as LINQ-To-Objects. Similarly, we can use LINQ on any other data, be it in the XML form, SQL Data (LINQ-To-SQL) or Entities (LINQ-To-Entities)
Pros & cons of LINQ
Let’s check some advantages of using LINQ:
Improves code readability
Provides compile-time object type-checking
Provides IntelliSense support for generic collection
LINQ queries can be reused
Provides in-built methods to write less code and expedite development
Provides common query syntax for various data sources
There are also disadvantages of using LINQ:
Difficult to write complex queries as SQL
Performance degradation if queries are not written accurately
Require to recompile, and redeploy every time a change is made to the query
Doesn’t take full advantage of SQL features such as cached execution plan for stored procedure
var words = new string[] { "falcon", "eagle", "sky", "tree", "water" };
// Method syntax
var res = words.Where(word => word.Contains("a")); //Lambda expression
foreach (var word in res)
Console.WriteLine(word);
Console.ReadKey();
var words = new string[] { "falcon", "eagle", "sky", "tree", "water" };
IEnumerable<string> res = words.Where(word => word.Contains("a"));
foreach (var word in res)
Console.WriteLine(word);
Console.ReadKey();
falcon
eagle
water
int[] nums = { 1, 2, 3, 4, 5, 6, 7, 8, 9};
var res = nums.Where(n => n % 2 == 0);
foreach (var num in res)
Console.WriteLine(num);
Console.ReadKey();