Linq

2,617 views
2,554 views

Published on

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

No Downloads
Views
Total views
2,617
On SlideShare
0
From Embeds
0
Number of Embeds
15
Actions
Shares
0
Downloads
87
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Linq

  1. 1. LINQ <br />By <br />PranayRana<br />Blog : http://pranayamr.blogspot.com<br />Twitter : http://twitter.com/pranayamr<br />FaceBook : http://www.facebook.com/pages/GMind-Solution<br />
  2. 2. Overview<br />Implicitly typed local variables <br />Anonymous Types<br />Object and Collection Initialization<br />Extension Method<br />Lambda Expression<br />Linq<br />
  3. 3. Virtual Diagram<br />LINQ<br />Anonymos<br />Types<br />Implicitly typed local variables <br /> Extension<br /> Method<br />Object and Collection Initialization<br />Lambda Expression<br />
  4. 4. Implicitly typed local variables<br /><ul><li>Syntax</li></ul>var i = 5;<br />var s = "Hello";<br />var d = 1.0;<br />var numbers = new int[] {1, 2, 3};<br />var orders = new <br /><ul><li> Easy to use
  5. 5. No need to define type </li></li></ul><li>Restrictions<br />The declarator must include an initializer.<br />The initializer must be an expression. The initializer cannot be an object or collection initializer by itself, but it can be a new expression that includes an object or collection initializer.<br />The compile-time type of the initializer expression cannot be the null type.<br />If the local variable declaration includes multiple declarators, the initializers must all have the same compile-time type.<br />
  6. 6. var x; // Error, no initializer to infer type from<br />var y = {1, 2, 3}; // Error, collection initializer not permitted<br />var z = null; // Error, null type not permitted<br />
  7. 7. Anonymous Types<br />What is Anonymous types ?<br />Create new types without defining it<br />How to define Anonymous types ?<br />var pranay = new { id=1, Name= "pranay rana" };<br />var krunal = new { id=1, Name= "krunal mevada" };<br />Use of it<br />var user = from user in Users <br /> select new { user.Name, user.Id};<br />
  8. 8. Object and Collection Initialization<br />What is object Initialization ?<br />create object without writing too much lengthy code and without invoking constructor.<br />Example<br />class Employee<br />{<br />  public string Name { get; set; }<br />  public string Address { get; set; }<br /> <br /> public Employee(){}<br /> public Employee (string name) { Name= name;  }<br />}<br />
  9. 9. Employee emp = new Employee() { Name="Pranay", Address="Ahmedabad"  };<br />or<br />Employee emp = new Employee("Hemang") { Address="Ahmedabad"   };<br />
  10. 10. Collection Initialization<br />List<Employee> emp = new List<Employee><br />      {<br />         new Employee { Name="Pranay", <br /> Address="Ahmedabad"  },<br />         new Employee { Name="Krunal",<br />Address="Mevada"  },<br />         null<br />     };<br />
  11. 11. Extension Method<br />What is Extesion Methods ?<br />Allow programmer to "add" methods to existing types without creating a new derived type, recompiling, or by modifying the original type<br />Example<br />public static class Utilities<br />{<br />    public static string encryptString(this string str)<br />    {<br />           System.Security.Cryptography.MD5CryptoServiceProvider x = <br /> new      System.Security.Cryptography.MD5CryptoServiceProvider();<br />           byte[] data = System.Text.Encoding.ASCII.GetBytes(str);<br />           data = x.ComputeHash(data);<br />           return System.Text.Encoding.ASCII.GetString(data);<br />     }<br />}<br />
  12. 12.
  13. 13. Lambda Expression<br />Replacement of the anonymous method available in C#2.0<br />Consist of single line or block of statement<br />=> is lamda expression operator<br />Syntax<br />( param ) => expr(int x) = > { return x + 1 };<br />param => exprx=> x + 1;<br />
  14. 14. Use of Lambda expression<br />C#2.0 by using anonymous method<br /> List<int> even = <br />     lstNumber.FindAll( delegate(inti) { if(i%2==) <br /> return i; }<br />But for the C#3.0 with the lambda expression it will<br /> List<int> even <br />     = lstNumber.FindAll( i => (i%2) ==0 }<br />In LINQ<br />var order = orders.where( o=> o.ProcessStatus  = <br />ProcessStatus.Completed);<br />
  15. 15. What is orm ?<br />Object Relational Mapping <br />Allow to map your database structure with the business layer model <br />Hide low level database interaction details<br />Make development easy and faster<br />
  16. 16. LINQ<br />What is LINQ ?<br />Language-Integrated Query<br /> Made up to make query any source of data<br />Some flavor of LINQ<br />LINQ to Object {Queries performed against the in-memory data}<br />LINQ to SQL (formerly DLinq) {Queries performed against the relation database only Microsoft SQL Server Supported}<br />LINQ to DataSet {Supports queries by using ADO.NET data sets and data tables}<br />LINQ to Entities {Microsoft ORM solution}<br />LINQ to XML (formerly XLinq) { Queries performed against the XML source}<br />
  17. 17.
  18. 18. Select<br />SQL<br />SELECT * FROM [User]<br /><ul><li>LINQ
  19. 19. var user = from u in Users select u;</li></li></ul><li>
  20. 20. Select with Columns<br />SQL<br /> Select FirstName, LastName from [User]<br /><ul><li>LINQ</li></ul>from u in Users <br />select new<br /> { u.FirstName, u.LastName };<br />
  21. 21.
  22. 22. Filter Selected Data<br />SQL<br />Select firstname,LastName from [User] where id = 3<br /><ul><li>LINQ</li></ul>from u in Users <br />where u.Id ==3<br />select new<br /> { u.FirstName, u.LastName };<br />
  23. 23.
  24. 24. Filtering String Data<br />SQL<br />SELECT [Id], [FirstName], [LastName], [Email], [DisplayName], [Address1], [Address2], [Password], [Role] FROM [User] WHERE [Email] LIKE '%pranay%'<br /><ul><li>LINQ</li></ul> from u in Users where u.Email.Contains ("pranay") select u;<br />Or<br /> from u in Users where u.Email.StartsWith ("pranay") select u;<br />
  25. 25.
  26. 26. Inner Join<br />SQL<br />SELECT [User].[Id], [FirstName], [LastName], [UserId], [MobileNo] FROM [User] INNER JOIN [UserClients] ON [User].[id] = [UserId]<br /><ul><li>LINQ</li></ul>var user = from u in Users <br /> join uc in UserClients on u.Id equals uc.UserId select new <br /> { <br />u.Id, u.FirstName, u.LastName, uc.MobileNo, <br />uc.imeiNO, uc.Id, <br /> };<br />
  27. 27.
  28. 28. Outer Join<br />SQL<br />SELECT [t0].[Id], [FirstName], [LastName], [UserId] AS [UserId], [MobileNo] AS [MobileNo] FROM [User] AS [t0] LEFT OUTER JOIN [UserClients] ON ([t0].[id]) = [UserId]<br /><ul><li>LINQ</li></ul>var user = from u in Users<br /> join uc in UserClients on u.Id equals uc.UserId into myuserwithclient from m in myuserwithclient.DefaultIfEmpty() <br />select new <br />{ u.Id, u.FirstName, u.LastName, m.UserId, m.MobileNo };<br />
  29. 29.
  30. 30. Ordering Data<br />SQL<br />Select * from [User] order by firstName<br /><ul><li>LINQ</li></ul>var user = from u in Users<br />orderbyu.FirstName<br />select new <br /> { u.FirstName, u.LastName };<br /><ul><li>More than one column</li></ul>varemp = from e in dc.Employeesorderbye.Name, e.Desc select e;<br />
  31. 31.
  32. 32. Grouping Data<br />SQL<br />SELECT COUNT(*) AS [test], [UserId] FROM [UserClients] GROUP BY [UserId]<br /><ul><li>LINQ</li></ul>var user = from u in UserClients<br /> group u by u.UserId<br /> into c <br /> select new <br /> { t1 = c.Key, tcount = c.Count() };<br />
  33. 33.
  34. 34. Filter Data Using IN and NOT IN Clauses<br />SQL<br />SELECT [Id], [UserId], [IMEINo] FROM [UserClients] WHERE [UserId] IN (3, 4)<br /><ul><li>LINQ</li></ul>int[] chosenOnes = { 3, 4 };<br />var user = from u in UserClients<br /> where chosenOnes.Contains(u.UserId.Value) <br /> select new <br /> { u.id,u.userid, u.ImeiNo};<br />
  35. 35.
  36. 36. Filtering Data by Row Numbers<br />SQL<br />SELECT * FROM ( SELECT ROW_NUMBER() OVER (ORDER BY [id]) AS [ROW_NUMBER], [id], [FirstName], [LastName], [Email], [DisplayName], [Address1], [Address2], [Password], [Role] FROM [User] AS [t0] ) AS [t1] WHERE [t1].[ROW_NUMBER] <br />BETWEEN 11 AND 20 <br />ORDER BY [t1].[ROW_NUMBER]<br /><ul><li>LINQ</li></ul>var users = from u in Users select u; <br />varfilterUsers= <br />users.OrderBy (p => p.Id).Skip (10).Take(10);<br />
  37. 37.
  38. 38. SQL ISNULL function<br />LINQ<br />var user = from u in Users join uc in UserClients on u.Id equals uc.UserId into myuserwithclient from m in myuserwithclient.DefaultIfEmpty() select new<br /> { <br />u.Id, <br />FirstName= u.FirstName, <br />LastName= u.LastName, <br />UserId= m.UserId, <br />MobileNo= m.MobileNo?? "N/A" <br />};<br />
  39. 39. Selectmany<br />Flattens the resulting sequences into one sequence<br />users.SelectMany (<br />      u => u.Userclient, <br />      (u, uc) => <br />         new <br />         {<br />            Name = u.Name, <br />            Mobileno = uc.Mobile, <br />            IsApproved = uc.IsApproved<br />         }<br />   ) <br />
  40. 40. Linq Deferred Execution<br />Query not going to execute at this point<br /> var query = <br /> from customer in db.Customers where <br />customer.City == "Paris" select customer; <br />Query get executed when we apply method like (Count(),toList() etc.)<br />int count = <br /> (from customer in db.Customers where customer.City == "Paris" select customer).Count();<br />
  41. 41. Questions <br />and <br />Query<br />
  42. 42. Thank YOU<br />

×