linq

51
Data can be stored in various places

Upload: foyzul-karim

Post on 20-Dec-2014

480 views

Category:

Technology


3 download

DESCRIPTION

This file is for starting the LINQ. Not the details of LINQ or How it works.

TRANSCRIPT

Page 1: Linq

Data can be stored in various places

Page 2: Linq

Where???

• Database (MSSQL, MySQL, Oracle, SQLite etc)

Page 3: Linq

Where???

• Database (MSSQL, MySQL, Oracle, SQLite etc)• XML

Page 4: Linq

Where???

• Database (MSSQL, MySQL, Oracle, SQLite etc)• XML• File  (Access, Excel etc)

Page 5: Linq

Where???

• Database (MSSQL, MySQL, Oracle, SQLite etc)• XML• File  (Access, Excel etc)• RAM ( Object, Array etc)

Page 6: Linq

But..How can we interact with these data?

Page 7: Linq

To work with database, we need

• Connection• Query• Execution procedure etc

Page 8: Linq

Working with ADO.NET

• DataTable• DataSet • SqlDataAdapter etc

Page 9: Linq

Working with RAM data

• Create user defined class• Load its properties with data

Page 10: Linq

So? Whats the point?

Different working procedure with different syntax

Page 11: Linq

             Find out the differences

            var query = from studentObject in students

            select studentObject;

            var query = from studentObject in db.students

            select studentObject;

Page 12: Linq

             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?

Page 13: Linq

LINQ

Language Integrated Query

Page 14: Linq

Who Am I?

NO. I am not talking about the Movie of Jackie Chan

Page 15: Linq

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

Page 16: Linq

What will you get from today's class?• Basic knowledge of LINQ• Can start working on LINQ (Important)

Page 17: Linq

How it works?

In simplified english:

From variable in Source

In syntax:

from customer in Customers

Page 18: Linq

How it works?

In simplified english:

From variable in Sourcewhere the condition is

true

In syntax:

from customer in Customerswhere customer.Name == "Foysal"

Page 19: Linq

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

Page 20: Linq

Types of LINQ

• LINQ to Object• LINQ to SQL• LINQ to XML

Page 21: Linq

Today's topics

• LINQ to Object• LINQ to SQL 

Page 22: Linq

LINQ To Object

Page 23: Linq

Initialization Expression:

By using an object initialization expression, we can initialize an object without calling its constructor and not setting its properties.

Page 24: Linq

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";

Page 25: Linq

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"};

Page 26: Linq

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.

Page 27: Linq

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.

Page 28: Linq

Working with LINQ

Click LinqSourceObjects to get the class library

Page 29: Linq

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;

Page 30: Linq

 

Practice:1.Get the student list who live in Shamoli.2.Get the names which has the length of more than 6

Page 31: Linq

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.

Page 32: Linq

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;

Page 33: Linq

 

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"

Page 34: Linq

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;

Page 35: Linq

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};

Page 36: Linq

 

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.

Page 37: Linq

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 

Page 38: Linq

What else we can do with it? (Cont.)• Quantifiers• Aggregate Operators • Miscellaneous Operators• Custom Sequence Operators• Query Execution• Join Operators• Utility Routines

Page 39: Linq

LINQ To SQL

Page 40: Linq

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. 

Page 41: Linq

Step 1: Add a "LINQ to SQL" class

Page 42: Linq

Step 2: Drag and Drop Tables to your Project

Page 43: Linq

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. ;)

Page 44: Linq

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.

 

Page 45: Linq

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.

Page 46: Linq

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();

Page 47: Linq

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();            }

Page 48: Linq

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();            }

Page 49: Linq

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);            }

Page 50: Linq

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);

            }

Page 51: Linq

Thats all for now.

Questions???

Or mail me ([email protected])

if you have questions later