Data can be stored in various places
Where??? <ul><ul><li>Database (MSSQL, MySQL, Oracle, SQLite etc) </li></ul></ul>
Where??? <ul><ul><li>Database (MSSQL, MySQL, Oracle, SQLite etc) </li></ul></ul><ul><ul><li>XML </li></ul></ul>
Where??? <ul><ul><li>Database (MSSQL, MySQL, Oracle, SQLite etc) </li></ul></ul><ul><ul><li>XML </li></ul></ul><ul><ul><li...
Where??? <ul><ul><li>Database (MSSQL, MySQL, Oracle, SQLite etc) </li></ul></ul><ul><ul><li>XML </li></ul></ul><ul><ul><li...
But..How can we interact with these data?
To work with database, we need <ul><ul><li>Connection </li></ul></ul><ul><ul><li>Query </li></ul></ul><ul><ul><li>Executio...
Working with ADO.NET <ul><ul><li>DataTable </li></ul></ul><ul><ul><li>DataSet  </li></ul></ul><ul><ul><li>SqlDataAdapter e...
Working with RAM data <ul><ul><li>Create user defined class </li></ul></ul><ul><ul><li>Load its properties with data </li>...
So? Whats the point? Different working procedure with different syntax
              Find out the differences <ul><li>             var query = from studentObject in students </li></ul><ul><li> ...
              Find out the differences <ul><li>             var query = from studentObject in students </li></ul><ul><li> ...
LINQ Language Integrated Query
Who Am I? NO. I am not talking about the Movie of Jackie Chan
Foyzul Karim (Foysal) <ul><ul><li>Currently </li></ul></ul><ul><ul><ul><li>Programmer, Databiz Software Limited </li></ul>...
What will you get from today's class? <ul><ul><li>Basic knowledge of LINQ </li></ul></ul><ul><ul><li>Can start working on ...
How it works? <ul><li>In simplified english: </li></ul><ul><li>From  variable  in  Source </li></ul>In syntax: from  custo...
How it works? <ul><li>In simplified english: </li></ul><ul><li>From  variable  in  Source </li></ul><ul><li>where  the con...
How it works? <ul><li>In simplified english: </li></ul><ul><li>from  variable  in  Source </li></ul><ul><li>where  the con...
Types of LINQ <ul><ul><li>LINQ to Object </li></ul></ul><ul><ul><li>LINQ to SQL </li></ul></ul><ul><ul><li>LINQ to XML </l...
Today's topics <ul><ul><li>LINQ to Object </li></ul></ul><ul><ul><li>LINQ to SQL  </li></ul></ul>
LINQ To Object
Initialization Expression: <ul><li>By using an object initialization expression, we can initialize an object without calli...
Initialization Expression: <ul><li>By using an object initialization expression, we can initialize an object without calli...
Initialization Expression: <ul><li>By using an object initialization expression, we can initialize an object without calli...
Anonymous types <ul><li>By using the anonymous type, we don't have to create a prototype (or we can call it as a class) of...
Anonymous types <ul><li>By using the anonymous type, we don't have to create a prototype (or we can call it as a class) of...
Working with LINQ Click  LinqSourceObjects  to get the class library
Restriction Operator : Where <ul><li>By this operator, we filter the source of data according to our need. </li></ul><ul><...
  <ul><li>Practice: </li></ul><ul><ul><li>Get the student list who live in Shamoli. </li></ul></ul><ul><ul><li>Get the nam...
How to work well in .NET <ul><li>To work better with a tool, you must know what can you do with that tool. Remember, Langu...
Restriction Operator : Where (Continuted) <ul><li>Another Example: </li></ul><ul><li>Primitive data types: </li></ul><ul><...
  <ul><li>Practice: </li></ul><ul><ul><li>Find the names which contains the string sequence of &quot;on&quot; </li></ul></...
Projection Operator: Select <ul><li>By this operator, we select our data in different ways. </li></ul><ul><li>Example: </l...
Projection Operator: Select (Continued) <ul><li>Example: </li></ul><ul><li>Primitive data types: </li></ul><ul><li>var sel...
  <ul><li>Practice: </li></ul><ul><li>1. Find the courses which are taken by the students who lives in Shamoli. </li></ul>...
What else we can do with it? <ul><li>There are several more operator in LINQ. Such as: </li></ul><ul><ul><li>Partitioning ...
What else we can do with it? (Cont.) <ul><ul><li>Quantifiers </li></ul></ul><ul><ul><li>Aggregate Operators  </li></ul></u...
LINQ To SQL
Why &quot;LINQ To SQL&quot;? <ul><li>LINQ to SQL provides a runtime infrastructure for managing relational data as objects...
Step 1: Add a &quot;LINQ to SQL&quot; class
Step 2: Drag and Drop Tables to your Project
Step 3: Start working :) <ul><ul><li>Add a Gateway/DAL class to perform operations on database (UniversityDAL.cs) </li></u...
Go back 3 slide and then return <ul><ul><li>Selected class from the image at right (BASISOOP0124DataClasses.dbml) contains...
The DataContext <ul><ul><li>The purpose of the DataContext is to translate your requests for objects into SQL queries made...
CRUD Operations: Insert <ul><li>                 t_Student studentObject = new t_Student(); </li></ul><ul><li>            ...
CRUD Operations: Update <ul><li>             var tempStudent = from student in odb.t_Students </li></ul><ul><li>          ...
CRUD Operations: Delete <ul><li>             var tempStudent = from student in odb.t_Students </li></ul><ul><li>          ...
CRUD Operations: Select <ul><li>         public IQueryable<t_Student> GetStudents() </li></ul><ul><li>         { </li></ul...
Joining <ul><li>             var query = from student in odb.t_Students </li></ul><ul><li>                         join co...
Thats all for now. Questions??? Or mail me ( [email_address] ) if you have questions later
Upcoming SlideShare
Loading in...5
×

Linq

491

Published on

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

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
491
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
30
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Linq

  1. 1. Data can be stored in various places
  2. 2. Where??? <ul><ul><li>Database (MSSQL, MySQL, Oracle, SQLite etc) </li></ul></ul>
  3. 3. Where??? <ul><ul><li>Database (MSSQL, MySQL, Oracle, SQLite etc) </li></ul></ul><ul><ul><li>XML </li></ul></ul>
  4. 4. Where??? <ul><ul><li>Database (MSSQL, MySQL, Oracle, SQLite etc) </li></ul></ul><ul><ul><li>XML </li></ul></ul><ul><ul><li>File  (Access, Excel etc) </li></ul></ul>
  5. 5. Where??? <ul><ul><li>Database (MSSQL, MySQL, Oracle, SQLite etc) </li></ul></ul><ul><ul><li>XML </li></ul></ul><ul><ul><li>File  (Access, Excel etc) </li></ul></ul><ul><ul><li>RAM ( Object, Array etc) </li></ul></ul>
  6. 6. But..How can we interact with these data?
  7. 7. To work with database, we need <ul><ul><li>Connection </li></ul></ul><ul><ul><li>Query </li></ul></ul><ul><ul><li>Execution procedure etc </li></ul></ul>
  8. 8. Working with ADO.NET <ul><ul><li>DataTable </li></ul></ul><ul><ul><li>DataSet  </li></ul></ul><ul><ul><li>SqlDataAdapter etc </li></ul></ul>
  9. 9. Working with RAM data <ul><ul><li>Create user defined class </li></ul></ul><ul><ul><li>Load its properties with data </li></ul></ul>
  10. 10. So? Whats the point? Different working procedure with different syntax
  11. 11.               Find out the differences <ul><li>             var query = from studentObject in students </li></ul><ul><li>             select studentObject; </li></ul><ul><li>             var query = from studentObject in db.students </li></ul><ul><li>             select studentObject; </li></ul>
  12. 12.               Find out the differences <ul><li>             var query = from studentObject in students </li></ul><ul><li>         select studentObject; </li></ul><ul><li>             var query = from studentObject in db.students </li></ul><ul><li>             select studentObject; </li></ul>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?
  13. 13. LINQ Language Integrated Query
  14. 14. Who Am I? NO. I am not talking about the Movie of Jackie Chan
  15. 15. Foyzul Karim (Foysal) <ul><ul><li>Currently </li></ul></ul><ul><ul><ul><li>Programmer, Databiz Software Limited </li></ul></ul></ul><ul><ul><ul><li>Do Outsourcing </li></ul></ul></ul><ul><ul><li>Previously </li></ul></ul><ul><ul><ul><li>Assistant Trainer of BASIS  </li></ul></ul></ul><ul><ul><ul><li>Newage Infotech Services Limited </li></ul></ul></ul><ul><ul><ul><li>Liveoutsource Limited </li></ul></ul></ul>[email_address] Graduated from RUET at May 2009
  16. 16. What will you get from today's class? <ul><ul><li>Basic knowledge of LINQ </li></ul></ul><ul><ul><li>Can start working on LINQ (Important) </li></ul></ul>
  17. 17. How it works? <ul><li>In simplified english: </li></ul><ul><li>From variable in Source </li></ul>In syntax: from customer in Customers
  18. 18. How it works? <ul><li>In simplified english: </li></ul><ul><li>From variable in Source </li></ul><ul><li>where the condition is true </li></ul>In syntax: from customer in Customers where customer.Name == &quot;Foysal&quot;
  19. 19. How it works? <ul><li>In simplified english: </li></ul><ul><li>from variable in Source </li></ul><ul><li>where the condition is true </li></ul><ul><li>select data </li></ul>In syntax: from customer in Customers where  customer.Name == &quot;Foysal&quot; select customer
  20. 20. Types of LINQ <ul><ul><li>LINQ to Object </li></ul></ul><ul><ul><li>LINQ to SQL </li></ul></ul><ul><ul><li>LINQ to XML </li></ul></ul>
  21. 21. Today's topics <ul><ul><li>LINQ to Object </li></ul></ul><ul><ul><li>LINQ to SQL  </li></ul></ul>
  22. 22. LINQ To Object
  23. 23. Initialization Expression: <ul><li>By using an object initialization expression, we can initialize an object without calling its constructor and not setting its properties. </li></ul>
  24. 24. Initialization Expression: <ul><li>By using an object initialization expression, we can initialize an object without calling its constructor and not setting its properties. </li></ul>Standard object initialization Student student=new Student(); student.Id = &quot;001&quot;; student.Name = &quot;Foysal&quot;; student.Address = &quot;Dhaka&quot;;
  25. 25. Initialization Expression: <ul><li>By using an object initialization expression, we can initialize an object without calling its constructor and not setting its properties. </li></ul>Standard object initialization Student student=new Student(); student.Id = &quot;001&quot;; student.Name = &quot;Foysal&quot;; student.Address = &quot;Dhaka&quot;; Initialization expression Student student = new Student {Id = &quot;001&quot;, Name = &quot;Foysal&quot;, Address = &quot;Dhaka&quot;};
  26. 26. Anonymous types <ul><li>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. </li></ul>
  27. 27. Anonymous types <ul><li>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. </li></ul><ul><li>Example: </li></ul><ul><li>new {studentObject.Name,studentObject.Address}; </li></ul><ul><li>The compiler will create a class for our selected values. </li></ul>
  28. 28. Working with LINQ Click  LinqSourceObjects  to get the class library
  29. 29. Restriction Operator : Where <ul><li>By this operator, we filter the source of data according to our need. </li></ul><ul><li>Example: </li></ul><ul><li>Primitive data types: </li></ul><ul><li>var selectedNames = from name in names </li></ul><ul><li>                                     where name.Length == 6 </li></ul><ul><li>                                     select name; </li></ul><ul><li>User defined data types: </li></ul><ul><li>var selectedStudents = from student in students </li></ul><ul><li>                                       where student.Name.Length == 6 </li></ul><ul><li>                                       select student; </li></ul>
  30. 30.   <ul><li>Practice: </li></ul><ul><ul><li>Get the student list who live in Shamoli. </li></ul></ul><ul><ul><li>Get the names which has the length of more than 6 </li></ul></ul>
  31. 31. How to work well in .NET <ul><li>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. </li></ul><ul><li>Linq provides a lot of methods to work on. I am giving another example of restriction operator in the upcoming slide. </li></ul>
  32. 32. Restriction Operator : Where (Continuted) <ul><li>Another Example: </li></ul><ul><li>Primitive data types: </li></ul><ul><li>var selectedNames =  from name in names </li></ul><ul><li>                                   where name.EndsWith(&quot;l&quot;) </li></ul><ul><li>                                       select name; </li></ul><ul><li>User defined data types: </li></ul><ul><li>var selectedStudents = from student in students </li></ul><ul><li>                                   where student.Name.StartsWith(&quot;S&quot;) </li></ul><ul><li>                                       select student; </li></ul>
  33. 33.   <ul><li>Practice: </li></ul><ul><ul><li>Find the names which contains the string sequence of &quot;on&quot; </li></ul></ul><ul><ul><li>Find the student list who has address containing the string sequence of &quot;li&quot; </li></ul></ul>
  34. 34. Projection Operator: Select <ul><li>By this operator, we select our data in different ways. </li></ul><ul><li>Example: </li></ul><ul><li>Primitive data types: </li></ul><ul><li>var selectedNames = from name in names </li></ul><ul><li>                         where name.Contains(&quot;Foysal&quot;) </li></ul><ul><li>                         select name; </li></ul><ul><li>User defined data types: </li></ul><ul><li>var selectedStudents = from student in students </li></ul><ul><li>                                       where student.Department==&quot;CSE&quot; </li></ul><ul><li>                                       select student.Name; </li></ul>
  35. 35. Projection Operator: Select (Continued) <ul><li>Example: </li></ul><ul><li>Primitive data types: </li></ul><ul><li>var selectedStudents = from student in students </li></ul><ul><li>                                   from course in student.CourseTaken </li></ul><ul><li>                             where course.CourseName.StartsWith(&quot;C&quot;) </li></ul><ul><li>                                   select student; </li></ul><ul><li>User defined data types: </li></ul><ul><li>var selectedStudents = from student in students </li></ul><ul><li>                                   from course in student.CourseTaken </li></ul><ul><li>                             where course.CourseName.StartsWith(&quot;C&quot;) </li></ul><ul><li>                                 select  </li></ul><ul><li>                               new {student.Name, course.CourseName}; </li></ul>
  36. 36.   <ul><li>Practice: </li></ul><ul><li>1. Find the courses which are taken by the students who lives in Shamoli. </li></ul><ul><li>2. Find the course Credit of the courses which are taken by the students whose name are 6 character long. </li></ul>
  37. 37. What else we can do with it? <ul><li>There are several more operator in LINQ. Such as: </li></ul><ul><ul><li>Partitioning Operators </li></ul></ul><ul><ul><li>Ordering Operators  </li></ul></ul><ul><ul><li>Grouping Operators  </li></ul></ul><ul><ul><li>Set Operators  </li></ul></ul><ul><ul><li>Conversion Operators  </li></ul></ul><ul><ul><li>Element Operators  </li></ul></ul><ul><ul><li>Generation Operators  </li></ul></ul>
  38. 38. What else we can do with it? (Cont.) <ul><ul><li>Quantifiers </li></ul></ul><ul><ul><li>Aggregate Operators  </li></ul></ul><ul><ul><li>Miscellaneous Operators </li></ul></ul><ul><ul><li>Custom Sequence Operators </li></ul></ul><ul><ul><li>Query Execution </li></ul></ul><ul><ul><li>Join Operators </li></ul></ul><ul><ul><li>Utility Routines </li></ul></ul>
  39. 39. LINQ To SQL
  40. 40. Why &quot;LINQ To SQL&quot;? <ul><li>LINQ to SQL provides a runtime infrastructure for managing relational data as objects without losing the ability to query. </li></ul><ul><li>[ MSDN ] </li></ul><ul><li>LINQ allows us to access and modify the database tables by our programming language in object oriented way.  </li></ul>
  41. 41. Step 1: Add a &quot;LINQ to SQL&quot; class
  42. 42. Step 2: Drag and Drop Tables to your Project
  43. 43. Step 3: Start working :) <ul><ul><li>Add a Gateway/DAL class to perform operations on database (UniversityDAL.cs) </li></ul></ul><ul><ul><li>Create an object of the added &quot;LINQ to SQL&quot; class (BASISOOP0124DataClasses.dbml) </li></ul></ul><ul><ul><li>Start doing things you like. ;) </li></ul></ul>
  44. 44. Go back 3 slide and then return <ul><ul><li>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. </li></ul></ul><ul><ul><li>We need to create an object of it to do the operations. </li></ul></ul><ul><li>  </li></ul>
  45. 45. The DataContext <ul><ul><li>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. </li></ul></ul><ul><ul><li>  The DataContext enables language-integrated query by implementing the same operator pattern as the standard query operators such as Where and Select. </li></ul></ul>
  46. 46. CRUD Operations: Insert <ul><li>                 t_Student studentObject = new t_Student(); </li></ul><ul><li>                 studentObject.id = &quot;001&quot;; </li></ul><ul><li>                 studentObject.name = &quot;foysal&quot;; </li></ul><ul><li>                 studentObject.batch = &quot;2004&quot;; </li></ul><ul><li>                 studentObject.departmentId = &quot;cse&quot;; </li></ul><ul><li>                 odb.t_Students.InsertOnSubmit(studentObject); </li></ul><ul><li>                 odb.SubmitChanges(); </li></ul>
  47. 47. CRUD Operations: Update <ul><li>             var tempStudent = from student in odb.t_Students </li></ul><ul><li>                               where student.id == &quot;001&quot; </li></ul><ul><li>                               select student; </li></ul><ul><li>             if (tempStudent.Count()>0) </li></ul><ul><li>             { </li></ul><ul><li>                 t_Student studentObject = tempStudent.First();                 </li></ul><ul><li>                 studentObject.name = &quot;Karim&quot;; </li></ul><ul><li>                 studentObject.batch = &quot;2004&quot;; </li></ul><ul><li>                 studentObject.departmentId = &quot;cse&quot;;                </li></ul><ul><li>                 odb.SubmitChanges(); </li></ul><ul><li>             } </li></ul>
  48. 48. CRUD Operations: Delete <ul><li>             var tempStudent = from student in odb.t_Students </li></ul><ul><li>                               where student.id == &quot;001&quot; </li></ul><ul><li>                               select student; </li></ul><ul><li>             if (tempStudent.Count()>0) </li></ul><ul><li>             { </li></ul><ul><li>                 t_Student studentObject = tempStudent.First();                 </li></ul><ul><li>                 odb.t_Students.DeleteOnSubmit(studentObject); </li></ul><ul><li>                 odb.SubmitChanges(); </li></ul><ul><li>             } </li></ul>
  49. 49. CRUD Operations: Select <ul><li>         public IQueryable<t_Student> GetStudents() </li></ul><ul><li>         { </li></ul><ul><li>             BASISOOP0124DataClassesDataContext odb=  </li></ul><ul><li>                          new BASISOOP0124DataClassesDataContext();                         var students = from student in odb.t_Students </li></ul><ul><li>  select student; </li></ul><ul><li>             return students; </li></ul><ul><li>         } </li></ul><ul><li>   [in another class] </li></ul><ul><li>    var students = universityDALObject.GetStudents(); </li></ul><ul><li>             foreach (t_Student student in students) </li></ul><ul><li>             { </li></ul><ul><li>                 Console.WriteLine(student.name); </li></ul><ul><li>             } </li></ul>
  50. 50. Joining <ul><li>             var query = from student in odb.t_Students </li></ul><ul><li>                         join course in odb.t_Courses on student.courseId equals course.id </li></ul><ul><li>                         select new </li></ul><ul><li>                         { </li></ul><ul><li>                             StudentName = student.name, </li></ul><ul><li>                             CourseName = course.name </li></ul><ul><li>                         }; </li></ul><ul><li>             foreach (var variable in query) </li></ul><ul><li>             { </li></ul><ul><li>                 Console.WriteLine(&quot;Student Name: &quot; + variable.StudentName + &quot; Course Name: &quot; + variable.CourseName); </li></ul><ul><li>             } </li></ul>
  51. 51. Thats all for now. Questions??? Or mail me ( [email_address] ) if you have questions later
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×