linq
DESCRIPTION
This file is for starting the LINQ. Not the details of LINQ or How it works.TRANSCRIPT
Data can be stored in various places
Where???
• Database (MSSQL, MySQL, Oracle, SQLite etc)
Where???
• Database (MSSQL, MySQL, Oracle, SQLite etc)• XML
Where???
• Database (MSSQL, MySQL, Oracle, SQLite etc)• XML• File (Access, Excel etc)
Where???
• Database (MSSQL, MySQL, Oracle, SQLite etc)• XML• File (Access, Excel etc)• RAM ( Object, Array etc)
But..How can we interact with these data?
To work with database, we need
• Connection• Query• Execution procedure etc
Working with ADO.NET
• DataTable• DataSet • SqlDataAdapter etc
Working with RAM data
• Create user defined class• Load its properties with data
So? Whats the point?
Different working procedure with different syntax
Find out the differences
var query = from studentObject in students
select studentObject;
var query = from studentObject in db.students
select studentObject;
Find out the differences
var query = from studentObject in students
select studentObject;
var query = from studentObject in db.students
select studentObject;
The left query is used to select data from a list of students and the right query is used to retrieve data from student table of a database. But the syntax is almost same.
Amazing. Isn't it?
LINQ
Language Integrated Query
Who Am I?
NO. I am not talking about the Movie of Jackie Chan
Foyzul Karim (Foysal)
• Currentlyo Programmer, Databiz Software Limitedo Do Outsourcing
• Previouslyo Assistant Trainer of BASIS o Newage Infotech Services Limitedo Liveoutsource Limited
[email protected] from RUET at May 2009
What will you get from today's class?• Basic knowledge of LINQ• Can start working on LINQ (Important)
How it works?
In simplified english:
From variable in Source
In syntax:
from customer in Customers
How it works?
In simplified english:
From variable in Sourcewhere the condition is
true
In syntax:
from customer in Customerswhere customer.Name == "Foysal"
How it works?
In simplified english:
from variable in Sourcewhere the condition is
trueselect data
In syntax:
from customer in Customerswhere customer.Name == "Foysal"select customer
Types of LINQ
• LINQ to Object• LINQ to SQL• LINQ to XML
Today's topics
• LINQ to Object• LINQ to SQL
LINQ To Object
Initialization Expression:
By using an object initialization expression, we can initialize an object without calling its constructor and not setting its properties.
Initialization Expression:
By using an object initialization expression, we can initialize an object without calling its constructor and not setting its properties.
Standard object initialization
Student student=new Student();student.Id = "001";student.Name = "Foysal";student.Address = "Dhaka";
Initialization Expression:
By using an object initialization expression, we can initialize an object without calling its constructor and not setting its properties.
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"};
Anonymous types
By using the anonymous type, we don't have to create a prototype (or we can call it as a class) of our required data out of many of them.
Anonymous types
By using the anonymous type, we don't have to create a prototype (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.
Working with LINQ
Click LinqSourceObjects to get the class library
Restriction Operator : Where
By this operator, we filter the source of data according to our need.
Example:Primitive data types:var selectedNames = from name in names where name.Length == 6 select name;
User defined data types:var selectedStudents = from student in students where student.Name.Length
== 6 select student;
Practice:1.Get the student list who live in Shamoli.2.Get the names which has the length of more than 6
How to work well in .NET
To work better with a tool, you must know what can you do with that tool. Remember, Language (C# for example) 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 giving another example of restriction operator in the upcoming slide.
Restriction Operator : Where (Continuted)
Another Example:Primitive data types:var selectedNames = from name in names where name.EndsWith("l") select name;
User defined data types:var selectedStudents = from student in students where
student.Name.StartsWith("S") select student;
Practice:1.Find the names which contains the string sequence
of "on"2.Find the student list who has address containing the
string sequence of "li"
Projection Operator: Select
By this operator, we select our data in different ways.Example:Primitive data types:var selectedNames = from name in names where name.Contains("Foysal") select name;User defined data types:var selectedStudents = from student in students where
student.Department=="CSE" select student.Name;
Projection Operator: Select (Continued)
Example:Primitive data types:var selectedStudents = from student in students from course in
student.CourseTaken
where course.CourseName.StartsWith("C") select student;User defined data types:var selectedStudents = from student in students from course in
student.CourseTaken where
course.CourseName.StartsWith("C") select new {student.Name,
course.CourseName};
Practice:1. Find the courses which are taken by the students
who lives in Shamoli.2. Find the course Credit of the courses which are taken
by the students whose name are 6 character long.
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
What else we can do with it? (Cont.)• Quantifiers• Aggregate Operators • Miscellaneous Operators• Custom Sequence Operators• Query Execution• Join Operators• Utility Routines
LINQ To SQL
Why "LINQ To SQL"?
LINQ to SQL provides a runtime infrastructure for managing relational data as objects without losing the ability to query.
[MSDN]
LINQ allows us to access and modify the database tables by our programming language in object oriented way.
Step 1: Add a "LINQ to SQL" class
Step 2: Drag and Drop Tables to your Project
Step 3: Start working :)
• Add a Gateway/DAL class to perform operations on database (UniversityDAL.cs)
• Create an object of the added "LINQ to SQL" class (BASISOOP0124DataClasses.dbml)
• Start doing things you like. ;)
Go back 3 slide and then return
• Selected class from the image at right (BASISOOP0124DataClasses.dbml) contains the DataContext class we have used in our next slides to query on the database tables and columns.
• We need to create an object of it to do the operations.
The DataContext
• The purpose of the DataContext is to translate your requests for objects into SQL queries made against the database and then assemble objects out of the results.
• The DataContext enables language-integrated query by implementing the same operator pattern as the standard query operators such as Where and Select.
CRUD Operations: Insert
t_Student studentObject = new t_Student(); studentObject.id = "001"; studentObject.name = "foysal"; studentObject.batch = "2004"; studentObject.departmentId = "cse";
odb.t_Students.InsertOnSubmit(studentObject); odb.SubmitChanges();
CRUD Operations: Update
var tempStudent = from student in odb.t_Students
where student.id == "001" select student; if (tempStudent.Count()>0) { t_Student studentObject =
tempStudent.First(); studentObject.name = "Karim"; studentObject.batch = "2004"; studentObject.departmentId = "cse";
odb.SubmitChanges(); }
CRUD Operations: Delete
var tempStudent = from student in odb.t_Students
where student.id == "001" select student; if (tempStudent.Count()>0) { t_Student studentObject =
tempStudent.First();
odb.t_Students.DeleteOnSubmit(studentObject); odb.SubmitChanges(); }
CRUD Operations: Select
public IQueryable<t_Student> GetStudents() { BASISOOP0124DataClassesDataContext odb= new
BASISOOP0124DataClassesDataContext(); var students = from student in odb.t_Students
select student; return students; } [in another class] var students = universityDALObject.GetStudents(); foreach (t_Student student in students) { Console.WriteLine(student.name); }
Joining
var query = from student in odb.t_Students join course in odb.t_Courses on
student.courseId equals course.id select new { StudentName = student.name, CourseName = course.name }; foreach (var variable in query) { Console.WriteLine("Student Name: " +
variable.StudentName + "\t Course Name: " + variable.CourseName);
}