What is the Linq
LINQ uses an SQL-like syntax to make query expressions well beyond the capabilities of embedded SQL as implemented in programming languages. LINQ is a set of extensions to the .NET Framework that encompass language-integrated query, set, and transform operations. It extends C# and Visual Basic with native language syntax for queries and provides class libraries to take advantage of these capabilities.
- Linq allows us to query a collection implementing IEnumerable<T> with SQL like syntax.
- We can write condition criteria, do the sorting and so on.
- A Linq query has two parts. The first is data and the other is query operators.
- The data could be an array, a list.
- The operators are defined as static extension methods in
System.Linq.Enumerable
.
- LINQ also supports data from a remote server, such as a SQL Server.
Lambda and Linq
Most query operators accept a lambda expression as an argument.which provide developers with a convenient way to write functions that can be passed as arguments for subsequent evaluation. Lambda expressions are similar to CLR delegates and must adhere to a method signature defined by a delegate type.
For example
C# code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication20
{
class Program
{
static void Main(string[] args)
{
string[] names = { "VisualBasic", "Java", "Csharp", "Javascript" };
IEnumerable<string> filteredNames = System.Linq.Enumerable.Where(names, n => n.Length >= 4);
foreach (string n in filteredNames)
Console.WriteLine(n);
}
}
}
In example above, the lambda expression is as follows:
n => n.Length >= 4
VB code
Module Module1
Sub Main()
Dim names As String() = {"Visual Basic", "Java", "Csharp", "Javascript"}
Dim filteredNames As IEnumerable(Of String) = System.Linq.Enumerable.Where(names,
Function(n) n.Length>= 4)
For Each n As String In filteredNames
Console.WriteLine(n)
Next
End Sub
End Module
OUTPUT
Query expression syntax
C# provides another syntax for writing queries, called query expression syntax.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void Main()
{
string[] names = { "VisualBasic", "Java", "C#", "Javascript" };
IEnumerable<string> filteredNames = from n in names select n;
foreach (string name in filteredNames)
Console.WriteLine(name);
}
}
VB code
Module Module1
Sub Main()
Dim names As String() = {"VisualBasic", "Java", "C#", "Javascript"}
Dim filteredNames As IEnumerable(Of String) = From n In names n
For Each name As String In filteredNames
Console.WriteLine(name)
Next
End Sub
End Module
OUTPUT
Lambda Expressions and Linq Operators
In example below, we are using the following lambda expression to the Where operator:
n => n.Contains ("a")
The input Input type is string, and the return type is bool.
The lambda expression depends on the particular query operator.
C# code
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void Main()
{
string[] names = { "C", "Java", "C#", "Javascript" };
IEnumerable<string> query = names
.Where(n => n.Contains("a"))
.OrderBy(n => n.Length)
.Select(n => n.ToUpper());
foreach (string name in query)
Console.WriteLine(name);
}
}
VB code
Module Module1
Sub Main()
Dim names As String() = {"C", "Java", "C#", "Javascript"}
Dim query As IEnumerable(Of String) = names.Where(Function(n) n.Contains("a")).OrderBy(Function(n) n.Length).[Select](Function(n) n.ToUpper())
For Each name As String In query
Console.WriteLine(name)
Next
End Sub
End Module
OUTPUT