 The full form of LINQ is 'Language Integrated Query,' and introduced in .NET Framework
3.5 to query the data from different sources of data such as collections, generics, XML
documents, ADO.NET Datasets, SQL, Web Services, etc.
 In C# and VB.NET. LINQ provides the rich, standardized query syntax in a .NET programming
language such as C# and VB.NET, which allows the developers to interact with any data sources.
 In C# or VB.NET, LINQ functionality can be achieved by importing the System.Linq namespace
in our application. Generally, the LINQ contains a set of extension methods which allows us to
query the source of data object directly in our code based on the requirement.
 LINQ
LINQ Architecture
The responsibility of the LINQ provider is to convert the LINQ
Query into a format so that the data source can understand it.
Advantages of LINQ
 We don’t required to learn new query language syntaxex from different sources of data
because LINQ provide standard query syntax.
 We have to write less code in comparison to traditional approach.
 LINQ provides the compile-time error checking as well as intelligence support in Visual
Studio. This powerful feature helps us to avoid run-time errors.
 LINQ provides a lot of built-in methods that we can be used to perform the different
operations such as filtering, ordering, grouping, etc. which makes our work easy.
 The query of LINQ can be reused.
 With the use of LINQ, it's very difficult to write a complex query like SQL.
 It was written in the code, and we cannot make use of the Cache Execution plan, which is the SQL
feature as we do in the stored procedure.
 If the query is not written correctly, then the performance will be degraded.
 If we make some changes to our queries, then we need to recompile the application and need to
redeploy the dll to the server.
Disadvantages of LINQ
Linq Syntax
 The requirement for writing the LINQ Query
To write the LINQ Query, we need the following three things:
 Data Source (in-memory objects, SQL, XML)
 Query
 Execution of the Query
 What is a Query?
A query is nothing but a set of instructions. Queries are applied to the data source (i.e., in-memory object,
SQL, XML, etc.) to perform the operations (i.e., CRUD operations) and show the shape of the output from
that Query. This means that the Query is not responsible for what will be the output; instead, it is responsible
for the shape of the output.
Each Query is a combination of three things; they are:
 Initialization(to work with a particular data source)
 Condition(where, filter, sorting condition)
 Selection (single selection, group selection or joining)
LINQ Lambda Expression
The syntax of defining the lambda expression in LINQ is as:
(Input Parameter) => Method Expression
The name of the parameter can be anything and ahead of this parameter (=>) is an Equal to (=) followed by
a greater than (>) symbol which is used to send or pass the parameter from left to right side, and on the
right-hand side we perform the operation using the input parameter which we will pass from the left-hand side
parameter.
Example: X=>x+10
Here, x is an input parameter which is followed by => operator, and next to the operator, there is an expression
that ads numeric 10 to the input variable (x). Now the output would increment the numeric 10 to x variable,
which was the input parameter on the left-hand side of the Expression.
 Aggregate Functions
 LINQ Min() Function and Max() Function
MIN () function of LINQ is useful to get the minimum value from a collection or list whereas Max
() function of LINQ is useful to get the maximum value
int[] a = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int m = a.Min(); or int m = a.Max();
From the above syntax, we are getting the minimum and maximum value from the "a" list using the
LINQ Min () or Max() function.
 LINQ Sum() Function
In LINQ sum() function is used to calculate the sum of the items in collections/lists.
int[] a = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int sum = a.Sum();
From the above syntax, we are summing up the items in the “a" list using the LINQ Sum function.
 LINQ Count() function
COUNT () function in LINQ is used to count the number of elements in the list or collection.
int[] a = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int m = a.count();
 LINQ Aggregate() Function
int[] Num = { 1, 2, 3, 4 };
double Average = Num.Aggregate((a, b) => a + b);
Output 10 ((1+2)+3)+4 )
in the above syntax, we take two elements 1 and 2 to perform the addition and make 3 then it take
the previous result 3 and next element 3 and perform the addition to make the 6 to the next
4 and result will be 10.
Ex. Now we will calculate the average of the numbers by applying the Aggregate function.
double Average = Num.Aggregate((a, b) => a * b);
Output 362880 ((((((((1*2)*3)*4)*5)*6)*7)*8)*9)
LINQ Sorting Operator
 Sorting Operators in LINQ are used to change the order or sequence of the data (either
ascending or descending), which is based on one or more attributes.
 LINQ OrderBy Operator(Ascending)
In LINQ, the OrderBy operator is used to sort the list/ collection values in ascending order. In
if we use order by the operator by default, it will sort the list of values in ascending order. We
need to add any ascending condition in the query statement.
Ex. var s = Obj.OrderBy(x => x.Name);
foreach (var student in s) Console.WriteLine(student.Name);
 LINQ OrderBy Descending Operator
In LINQ, the OrderBy operator is used to sort the list/ collection values in descending order.
Ex. var s = Obj.OrderByDescending (x => x.Name);
foreach (var student in s) Console.WriteLine(student.Name);
 LINQ ThenBy Operator
If we want more than one condition on sorting in LINQ, then we use the ThenBy clause with the
OrderBy clause. In LINQ, OrderBy is the primary sorting operator, and ThenBy is the secondary
operator.
In the above example, we are sorting the
"ObjStudent" list item using the multiple
fields Name, RoleNumber Id.
 LINQ ThenByDescending Operator
In LINQ, ThenByDescending operator is used to implement the sorting on multiple fields in the
list/ collection, and by default, ThenByDescending operator will sort the list of items in descending
order. In LINQ, we use the ThenByDescending operator along with OrderBy operator.
 Partition Operators
In LINQ, Partition Operators are used to partition the list/collections items into two parts and return one part of
the list items. Here are the different types of partitioning operators available in LINQ.
Take() and TakeWhile()
In LINQ, Take Operator is used to get the specified number of elements in sequence from the
list/collection. The LINQ takes the Operator will return the specified number of elements from the
starting of collection or list.
Ex. string[] countries = { "India", "USA", "Russia", "China", "Australia", "Argentina" };
IEnumerable<string> result = countries.Take(3);
foreach (string s in result) Console.WriteLine(s);
LINQ, TakeWhile Operator is used to get the elements from the list/collection of the data source as
long as the specified condition is holding the expression or until the condition is false.
Ex. string[] countries = { "India", "USA", "Russia", "China", "Australia", "Argentina" };
IEnumerable<string> result = (from x in countries select x).TakeWhile(x => x.StartsWith("U"));
foreach (string s in result) Console.WriteLine(s);
LINQ SKIP OPERATOR
In LINQ, the Skip operator is used to skip the specified number of elements from the list/collection
and return the remaining elements
Ex. string[] countries = { "India", "USA", "Russia", "China", "Australia", "Argentina" };
IEnumerable<string> result = countries.Skip(3);
foreach (string s in result) Console.WriteLine(s);

LINQ.pptx

  • 1.
     The fullform of LINQ is 'Language Integrated Query,' and introduced in .NET Framework 3.5 to query the data from different sources of data such as collections, generics, XML documents, ADO.NET Datasets, SQL, Web Services, etc.  In C# and VB.NET. LINQ provides the rich, standardized query syntax in a .NET programming language such as C# and VB.NET, which allows the developers to interact with any data sources.  In C# or VB.NET, LINQ functionality can be achieved by importing the System.Linq namespace in our application. Generally, the LINQ contains a set of extension methods which allows us to query the source of data object directly in our code based on the requirement.  LINQ
  • 2.
    LINQ Architecture The responsibilityof the LINQ provider is to convert the LINQ Query into a format so that the data source can understand it.
  • 3.
    Advantages of LINQ We don’t required to learn new query language syntaxex from different sources of data because LINQ provide standard query syntax.  We have to write less code in comparison to traditional approach.  LINQ provides the compile-time error checking as well as intelligence support in Visual Studio. This powerful feature helps us to avoid run-time errors.  LINQ provides a lot of built-in methods that we can be used to perform the different operations such as filtering, ordering, grouping, etc. which makes our work easy.  The query of LINQ can be reused.
  • 4.
     With theuse of LINQ, it's very difficult to write a complex query like SQL.  It was written in the code, and we cannot make use of the Cache Execution plan, which is the SQL feature as we do in the stored procedure.  If the query is not written correctly, then the performance will be degraded.  If we make some changes to our queries, then we need to recompile the application and need to redeploy the dll to the server. Disadvantages of LINQ
  • 5.
    Linq Syntax  Therequirement for writing the LINQ Query To write the LINQ Query, we need the following three things:  Data Source (in-memory objects, SQL, XML)  Query  Execution of the Query  What is a Query? A query is nothing but a set of instructions. Queries are applied to the data source (i.e., in-memory object, SQL, XML, etc.) to perform the operations (i.e., CRUD operations) and show the shape of the output from that Query. This means that the Query is not responsible for what will be the output; instead, it is responsible for the shape of the output.
  • 6.
    Each Query isa combination of three things; they are:  Initialization(to work with a particular data source)  Condition(where, filter, sorting condition)  Selection (single selection, group selection or joining)
  • 7.
    LINQ Lambda Expression Thesyntax of defining the lambda expression in LINQ is as: (Input Parameter) => Method Expression The name of the parameter can be anything and ahead of this parameter (=>) is an Equal to (=) followed by a greater than (>) symbol which is used to send or pass the parameter from left to right side, and on the right-hand side we perform the operation using the input parameter which we will pass from the left-hand side parameter. Example: X=>x+10 Here, x is an input parameter which is followed by => operator, and next to the operator, there is an expression that ads numeric 10 to the input variable (x). Now the output would increment the numeric 10 to x variable, which was the input parameter on the left-hand side of the Expression.
  • 8.
     Aggregate Functions LINQ Min() Function and Max() Function MIN () function of LINQ is useful to get the minimum value from a collection or list whereas Max () function of LINQ is useful to get the maximum value int[] a = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; int m = a.Min(); or int m = a.Max(); From the above syntax, we are getting the minimum and maximum value from the "a" list using the LINQ Min () or Max() function.  LINQ Sum() Function In LINQ sum() function is used to calculate the sum of the items in collections/lists. int[] a = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; int sum = a.Sum(); From the above syntax, we are summing up the items in the “a" list using the LINQ Sum function.
  • 9.
     LINQ Count()function COUNT () function in LINQ is used to count the number of elements in the list or collection. int[] a = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; int m = a.count();  LINQ Aggregate() Function int[] Num = { 1, 2, 3, 4 }; double Average = Num.Aggregate((a, b) => a + b); Output 10 ((1+2)+3)+4 ) in the above syntax, we take two elements 1 and 2 to perform the addition and make 3 then it take the previous result 3 and next element 3 and perform the addition to make the 6 to the next 4 and result will be 10. Ex. Now we will calculate the average of the numbers by applying the Aggregate function. double Average = Num.Aggregate((a, b) => a * b); Output 362880 ((((((((1*2)*3)*4)*5)*6)*7)*8)*9)
  • 10.
    LINQ Sorting Operator Sorting Operators in LINQ are used to change the order or sequence of the data (either ascending or descending), which is based on one or more attributes.
  • 11.
     LINQ OrderByOperator(Ascending) In LINQ, the OrderBy operator is used to sort the list/ collection values in ascending order. In if we use order by the operator by default, it will sort the list of values in ascending order. We need to add any ascending condition in the query statement. Ex. var s = Obj.OrderBy(x => x.Name); foreach (var student in s) Console.WriteLine(student.Name);  LINQ OrderBy Descending Operator In LINQ, the OrderBy operator is used to sort the list/ collection values in descending order. Ex. var s = Obj.OrderByDescending (x => x.Name); foreach (var student in s) Console.WriteLine(student.Name);
  • 12.
     LINQ ThenByOperator If we want more than one condition on sorting in LINQ, then we use the ThenBy clause with the OrderBy clause. In LINQ, OrderBy is the primary sorting operator, and ThenBy is the secondary operator. In the above example, we are sorting the "ObjStudent" list item using the multiple fields Name, RoleNumber Id.
  • 13.
     LINQ ThenByDescendingOperator In LINQ, ThenByDescending operator is used to implement the sorting on multiple fields in the list/ collection, and by default, ThenByDescending operator will sort the list of items in descending order. In LINQ, we use the ThenByDescending operator along with OrderBy operator.
  • 14.
     Partition Operators InLINQ, Partition Operators are used to partition the list/collections items into two parts and return one part of the list items. Here are the different types of partitioning operators available in LINQ.
  • 15.
    Take() and TakeWhile() InLINQ, Take Operator is used to get the specified number of elements in sequence from the list/collection. The LINQ takes the Operator will return the specified number of elements from the starting of collection or list. Ex. string[] countries = { "India", "USA", "Russia", "China", "Australia", "Argentina" }; IEnumerable<string> result = countries.Take(3); foreach (string s in result) Console.WriteLine(s); LINQ, TakeWhile Operator is used to get the elements from the list/collection of the data source as long as the specified condition is holding the expression or until the condition is false. Ex. string[] countries = { "India", "USA", "Russia", "China", "Australia", "Argentina" }; IEnumerable<string> result = (from x in countries select x).TakeWhile(x => x.StartsWith("U")); foreach (string s in result) Console.WriteLine(s);
  • 16.
    LINQ SKIP OPERATOR InLINQ, the Skip operator is used to skip the specified number of elements from the list/collection and return the remaining elements Ex. string[] countries = { "India", "USA", "Russia", "China", "Australia", "Argentina" }; IEnumerable<string> result = countries.Skip(3); foreach (string s in result) Console.WriteLine(s);