Professional Documents
Culture Documents
8 Linq
8 Linq
Chapter (7)
LINQ ( Language Integrated Query )
1
Contents
What is LINQ?
Query Syntax
Operators in Query Syntax
Operators in Method Syntax
2
LINQ ( Language Integrated Query )
It is a data retrieval mechanism.
It is used to filter, select and group data in various data structures such as array, list, collections, ADO.Net DataSet,
XML Docs, web service and MS SQL Server and other databases.
Execute Query
Returns Data
Objects { Linq Query} Source
Retrieve Result
3
LINQ ( Language Integrated Query )
The standard LINQ tools are divided into the following three broad categories:
LINQ to Objects LINQ methods that interact with C# objects such as arrays, dictionaries, and lists.
LINQ to ADO.NET LINQ features that let you write LINQ-style queries to extract data from relational databases
1. Query Syntax
2. Method Syntax
Most queries in the introductory Language Integrated Query (LINQ) documentation are written by using
the LINQ declarative query syntax.
Query syntax must be translated into method calls for the .NET common language runtime (CLR)
when the code is compiled.
These method calls invoke the standard query operators, which have names such as Where, Select,
GroupBy, Join, Max, and Average.
Standard query operators can be called directly by using method syntax instead of query syntax.
5
6
Query Syntax
7
Query Syntax
class Student
{
public int StudentID;
public string StudentName;
public int Age;
public string year;
public string Interested;
}
8
Query Syntax
ArrayList std = new ArrayList() {
new Student() { StudentID = 1, StudentName = "Su Su", Age = 13} ,
new Student() { StudentID = 2, StudentName = "Yu Yu", Age = 21 } ,
new Student() { StudentID = 3, StudentName = "Ma Ma", Age = 18 } ,
new Student() { StudentID = 4, StudentName = "Aung Aung" , Age = 20} ,
new Student() { StudentID = 5, StudentName = "Kyaw Kyaw" , Age = 15 },
new Student() { StudentID = 6, StudentName = "Ei Ei" , Age = 15 },
new Student() { StudentID = 7, StudentName = "Pa Pa" , Age = 15 }
};
select clause Lists the fields that the query should select into its result.
Syntax
from ....in var variablename = from queryVariable in dataSource
select … ;
select …..
Example
var result = from s in std
select s.StudentName;
10
From in Query Syntax
If you want to select more than one field from the query’s objects,
Select new clause
use the new keywords followed by the values enclosed in braces.
Syntax
Example
11
From in Query Syntax
Syntax
Example
var result = from s in std
from d in detail
select new { s.StudentName, d.Interested };
12
Where in Query Syntax
Syntax
Example
var result = from s in std var result = from s in std
where s.Age > 12 && s.Age < 20 from d in detail
select s.StudentName; where s.studentID == d.studentID
select new { s.StudentName, d.Interested };
13
Orderby in Query Syntax
Orderby clause Sorts a query’s results. Usually the values used to sort the results are properties of the objects selected.
Syntax
Example
var Result = from s in std var Result = from s in std
orderby s.StudentName orderby s.StudentName descending
select s.StudentName; select s.StudentName;
14
Join in Query Syntax
Join clause selects data from multiple data matching up corresponding field
Syntax
Join into clause add an “ into ” clause to the join clause to group the joined values into a list with a specified new
name.
Syntax
16
GroupJoin in Query Syntax
Example
}
17
GroupBy in Query syntax
group by clause Like join into, group by enables a program to gather related values together into groups.
Syntax
from ... in
from ....in
1. group….. by…..into ……..
group ….by… select ...
from ....in
2. where..
group ….by…
from ....in
3. where..
order by … descending
18
group ….by…
GroupBy in Query syntax
1. var variabaleName = from queryVariable in dataSource
group queryVariable by queryVariable.Key;
foreach (Student s in ageGroup) // Each group has inner collection // to loop each object in group
{
Console.WriteLine("Student Name: " + s.StudentName);
}
20
GroupBy in Query syntax
Example 2
var result = from Student s in std
group s by s.Age into StudentGp
select new { StudentAge = StudentGp.Key, Students = StudentGp };
21
GroupBy in Query syntax
Example 3
var result = from Student s in std
where s.Age > 18
group s by s.Age;
22
GroupBy in Query syntax
Example 4
var result2 = from Student s in std
where s.Age > 18
orderby s.Age
group s by s.Age;
}
} 23
24
OfType in Method Syntax
Example
ArrayList mixedList = new ArrayList();
mixedList.Add(0);
mixedList.Add("One");
mixedList.Add("Two");
mixedList.Add(3);
mixedList.Add(new Student() { StudentID = 1, StudentName = "Su Su", Age=20 });
25
Select in Method Syntax
Example
Method Syntax var result = std.Select ( x => new { x.StudentName, x.Age });
Method Syntax var result = std.Where(s => s.Age > 12 && s.Age < 20)
28
Orderby in Method Syntax
Example
Method Syntax
29
GroupBy in Method syntax
Example
var result = from Student s in std
Query Syntax group s by s.Age;
} 31
Join in Method syntax
Example
var result = from Student s in std
Query Syntax join Student d in detail
on s.StudentID equals d.StudentID
select new { s.StudentName, d.year, d.Interested };
var result = from string st1 in day1 var result = day1.Join ( day2,
join string st2 in day2 st1=>st1,
on st1 equals st2 st2=>st2,
select st1; ( st1, st2 )=>st1 );
All Operator bool result = std.All(s => s.Age > 12 && s.Age < 20);
Any Operator bool result = std.Any(s => s.Age >12 && s.Age< 20);
34
Aggregate operator in Method Syntax
);
35
Sum in Method Syntax
List<int> num = new List<int>() {10,20,22,55,48,93,70 };
37
Count in Method Syntax
Example
38
Count in Method Syntax
Example
double age = std.Max(s => s.Age); double age = std.Min(s => s.Age);
40
Distinct in Method Syntax
Example
List<string> name = new List<string>() { "Su Su", "Ma Ma", "Kyaw Kyaw", "Su Su", "Aung Aung", "Ma Ma", "Hla Hla" };
41
Union, Intersect in Method Syntax
Example
var st = day1.Concat(day2);
43
ElementAt, ElementAtOrDefault
in Method Syntax
Example
List<string> day1 = new List<string>() { "Monday", null, "Thursday" };
List<int> num1 = new List<int>() { 10, 20, 50 };
List<double> d = new List<double>();
day1.ElementAt(0) day1.ElementAtOrDefault(3)
num1.ElementAt(2) num1.ElementAtOrDefault(3)
d.ElementAtOrDefault(0)
44
Thank You
45