This document provides an introduction to LINQ (Language Integrated Query) for querying and manipulating data from different data sources. It discusses some key LINQ concepts and operators including projection, restriction, grouping, aggregation, and ordering. It also provides sample LINQ queries against a sample data source and compares the syntax to equivalent SQL queries. The document explains the different types of query execution in LINQ and provides additional resources for learning more.
2. Introduction
A query is an expression that retrieves data from a data source.
Queries are usually expressed in a specialized query language.
Different languages have been developed over time for the various
types of data sources,
for example
SQL for relational databases
XQuery for XML.
2LINQ for absolute beginners
4. Frequently used Operators
Projection
Select
Select Many
Restriction
Where
Grouping
GroupBy
Aggregate
Sum
Count
Min
Max
Average
Ordering
OrderBy
OrderByDescending
ThenBy
ThenByDescending
Reverse
4LINQ for absolute beginners
5. Steps involved in LINQ operation
Obtain the
Data Source
Create
Query
Execute
5LINQ for absolute beginners
6. Sample data source
EmpID EmpName Gender Age ShortCode
1 Kathirvel M 26 KV
2 Vivian M 27 OV
3 Veeramanai M 25 AV
4 Kalpana F 26 KL
5 Kavitha F 25 KS
6 Vasi M 28 SV
7 Kamalam F 28 KL
8 Sudha F 27 SS
9 Kumaraguru M 28 KG
6LINQ for absolute beginners
7. Projection Operator
SELECT e.EmpName FROM EmployeeInfo e
from e in EmployeeInfo select e.EmpName
SQL
LINQ Select
SELECT e.EmpName, e.Age FROM EmployeeInfo e
from e in EmployeeInfo
select new {EmpName = e.EmpName, Age = e.Age }
SQL
LINQ
SelectMany
EmpName
Kathirvel
Vivian
Veeramanai
Kalpana
Kavitha
Vasi
Kamalam
Sudha
Kumaraguru
EmpName Age
Kathirvel 26
Vivian 27
Veeramanai 25
Kalpana 26
Kavitha 25
Vasi 28
Kamalam 28
Sudha 27
Kumaraguru 28
7LINQ for absolute beginners
8. Restriction Operators
SELECT e.EmpName FROM EmployeeInfo e
WHERE e.Gender = M
from e in EmployeeInfo
where e.Gender == M
select e.EmpName
SQL
LINQ
Where
EmpName
Kathirvel
Vivian
Veeramanai
Vasi
Kumaraguru
8LINQ for absolute beginners
9. Grouping/Aggregate Operators
SELECT e.Gender, Count(e.*) as Employees FROM EmployeeInfo e
GROUP BY e.Gender
from e in EmployeeInfo
group e by e.Gender into grp
select new {Gender = e.Key, Employees = grp.Count()}
SQL
LINQ Grouping/
Aggregation
Gender Employees
F 4
M 5
9LINQ for absolute beginners
10. Ordering Operators
SELECT e.EmpName FROM EmployeeInfo e
ORDER BY e.EmpName
from e in EmployeeInfo
orderby e.EmpName
select e.EmpName
SQL
LINQ
OrderBy
10LINQ for absolute beginners
EmpName
Kalpana
Kamalam
Kathirvel
Kavitha
Kumaraguru
Sudha
Vasi
Veeramanai
Vivian
from e in EmployeeInfo
orderby e.EmpName descending
select e.EmpName OrderByDescending
EmpName
Vivian
Veeramanai
Vasi
Sudha
Kumaraguru
Kavitha
Kathirvel
Kamalam
Kalpana
11. Query Execution Types
Simple query for further operation
Deferred Query
Query that involves aggregate functions
Immediate Query
LINQ for absolute beginners 11
12. Thank you
Check the other resources
Blog
http://vasistan.blogspot.in
YouTube
http://www.youtube.com/playlist?list=PL-CcORxeaUcgXHO7UW9sGNbue9wH-4No3