linq to object
TRANSCRIPT
-
8/9/2019 LINQ to Object
1/28
Foyzul KarimLINQ to Object
-
8/9/2019 LINQ to Object
2/28
Why a businessman needssoftware?
To automate his business (data)
-
8/9/2019 LINQ to Object
3/28
Where does the data stored?
Database, RAM, XML, File etc
-
8/9/2019 LINQ to Object
4/28
Working with Database
SqlConnection Query Execution procedure etc
-
8/9/2019 LINQ to Object
5/28
Working with ADO.NET
DataTable DataSet SqlDataAdapter etc
-
8/9/2019 LINQ to Object
6/28
Working with RAM data
Create user defined class Load its properties with data Arrays etc
-
8/9/2019 LINQ to Object
7/28
Whats the point?
Different working procedure with differentsyntax
-
8/9/2019 LINQ to Object
8/28
Find out the differences
var query = fromstudentObject in students
select studentObject;
var query = fromstudentObject in db.students
select studentObject;
-
8/9/2019 LINQ to Object
9/28
Find out the differences
var query = fromstudentObject in students
select studentObject;
var query = fromstudentObject in db.students
select studentObject;
The left query is used to select data from a list of studentsand the right query is used to retrieve data from studenttable of a database. But the syntax is almost same.
Amazing. Isn't it?
-
8/9/2019 LINQ to Object
10/28
LINQ
Language Integrated Query
-
8/9/2019 LINQ to Object
11/28
How it works?
In simplified english:from variable in source
In syntax:
from customerObject in Customers
-
8/9/2019 LINQ to Object
12/28
How it works?
In simplified english:from variable in source
where the condition is true
In syntax:
from customerObject in Customerswhere customerObject.Name == "Foysal"
-
8/9/2019 LINQ to Object
13/28
How it works?
In simplified english:from variable in source
where the condition is trueselect data
In syntax:
from customerObject in Customerswhere customerObject.Name == "Foysal"select customerObject;
-
8/9/2019 LINQ to Object
14/28
Types of LINQ
LINQ to Object LINQ to SQL LINQ to XML
-
8/9/2019 LINQ to Object
15/28
Types of LINQ
LINQ to Object (Today's agenda) LINQ to SQL LINQ to XML
-
8/9/2019 LINQ to Object
16/28
LINQ To Object
-
8/9/2019 LINQ to Object
17/28
Initialization Expression:
Standard object initialization
Student student=new Student();student.Id = "001";
student.Name = "Foysal";student.Address = "Dhaka";
Initialization expression
Student student = new Student{Id = "001", Name = "Foysal",
Address = "Dhaka"};
By using an object initialization expression, we caninitialize an object without calling its constructor and notsetting its properties.
-
8/9/2019 LINQ to Object
18/28
Anonymous types
By using the anonymous type, we don't have to create aprototype (or we can call it as a class) of our required
data out of many of them.
Example:new {studentObject.Name,studentObject.Address};
The compiler will create a class for our selected values.
-
8/9/2019 LINQ to Object
19/28
Restriction Operator : Where
This operator allows us to filter our required data from acollection of data.
Example:
var selectedNames = from name in nameswhere name.Length == 6select name;
or
var selectedStudents = from student in studentswhere student.Name.Length == 6
select student;
-
8/9/2019 LINQ to Object
20/28
Practice:1. Get the student list who live in Shamoli.2. Get the names which has the length of more than 6
-
8/9/2019 LINQ to Object
21/28
How to work well in .NET
To work better with a tool, you must know what can youdo with that tool. Remember, Language (C# forexample) is just a tool for your coding. So, you have to
know what can you do with this tool.
Linq provides a lot of methods to work on. I am givinganother example of restriction operator in the upcomingslide.
-
8/9/2019 LINQ to Object
22/28
Example:var selectedNames = from name in names
where name.EndsWith("l")select name;
or
var selectedStudents = from student in studentswhere student.Name.StartsWith("S")
select student;
Restriction Operator : Where
-
8/9/2019 LINQ to Object
23/28
Practice:1. Find the names which contains the string sequence
of "on"
2. Find the student list who has address containing thestring sequence of "li"
-
8/9/2019 LINQ to Object
24/28
Projection Operator: Select
By this operator, we select our data in different ways.Example:var selectedNames = from name in names
where name.Contains("Foysal")
select name;
Or,
var selectedStudents = from student in students
where student.Department=="CSE"select student.Name;
-
8/9/2019 LINQ to Object
25/28
Example:var selectedStudents = from student in students
from course in student.CourseTakenwhere course.CourseName.StartsWith("C")
select student;Or,var selectedStudents = from student in students
from course in student.CourseTakenwhere course.CourseName.StartsWith("C")
select new {student.Name, course.CourseName};
Projection Operator: Select (Cont.)
-
8/9/2019 LINQ to Object
26/28
Practice:1. Find the courses which are taken by the students who livesin Shamoli.2. Find the course Credit of the courses which are taken by the
students whose name are 6 character long.
-
8/9/2019 LINQ to Object
27/28
What else we can do with it?There are several more operator in LINQ. Such as:
Partitioning Operators Ordering Operators Grouping Operators Set Operators Conversion Operators
Element Operators Generation Operators Quantifiers Aggregate Operators
Miscellaneous Operators Custom Sequence Operators Query Execution Join Operators Utility Routines
-
8/9/2019 LINQ to Object
28/28
Next Day: LINQ to SQL