Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Linq
1. LINQ By PranayRana Blog : http://pranayamr.blogspot.com Twitter : http://twitter.com/pranayamr FaceBook : http://www.facebook.com/pages/GMind-Solution
2. Overview Implicitly typed local variables Anonymous Types Object and Collection Initialization Extension Method Lambda Expression Linq
3. Virtual Diagram LINQ Anonymos Types Implicitly typed local variables Extension Method Object and Collection Initialization Lambda Expression
4.
5.
6. var x; // Error, no initializer to infer type from var y = {1, 2, 3}; // Error, collection initializer not permitted var z = null; // Error, null type not permitted
7. Anonymous Types What is Anonymous types ? Create new types without defining it How to define Anonymous types ? var pranay = new { id=1, Name= "pranay rana" }; var krunal = new { id=1, Name= "krunal mevada" }; Use of it var user = from user in Users select new { user.Name, user.Id};
8. Object and Collection Initialization What is object Initialization ? create object without writing too much lengthy code and without invoking constructor. Example class Employee { public string Name { get; set; } public string Address { get; set; } public Employee(){} public Employee (string name) { Name= name; } }
9. Employee emp = new Employee() { Name="Pranay", Address="Ahmedabad" }; or Employee emp = new Employee("Hemang") { Address="Ahmedabad" };
10. Collection Initialization List<Employee> emp = new List<Employee> { new Employee { Name="Pranay", Address="Ahmedabad" }, new Employee { Name="Krunal", Address="Mevada" }, null };
11. Extension Method What is Extesion Methods ? Allow programmer to "add" methods to existing types without creating a new derived type, recompiling, or by modifying the original type Example public static class Utilities { public static string encryptString(this string str) { System.Security.Cryptography.MD5CryptoServiceProvider x = new System.Security.Cryptography.MD5CryptoServiceProvider(); byte[] data = System.Text.Encoding.ASCII.GetBytes(str); data = x.ComputeHash(data); return System.Text.Encoding.ASCII.GetString(data); } }
12.
13. Lambda Expression Replacement of the anonymous method available in C#2.0 Consist of single line or block of statement => is lamda expression operator Syntax ( param ) => expr(int x) = > { return x + 1 }; param => exprx=> x + 1;
14. Use of Lambda expression C#2.0 by using anonymous method List<int> even = lstNumber.FindAll( delegate(inti) { if(i%2==) return i; } But for the C#3.0 with the lambda expression it will List<int> even = lstNumber.FindAll( i => (i%2) ==0 } In LINQ var order = orders.where( o=> o.ProcessStatus = ProcessStatus.Completed);
15. What is orm ? Object Relational Mapping Allow to map your database structure with the business layer model Hide low level database interaction details Make development easy and faster
16. LINQ What is LINQ ? Language-Integrated Query Made up to make query any source of data Some flavor of LINQ LINQ to Object {Queries performed against the in-memory data} LINQ to SQL (formerly DLinq) {Queries performed against the relation database only Microsoft SQL Server Supported} LINQ to DataSet {Supports queries by using ADO.NET data sets and data tables} LINQ to Entities {Microsoft ORM solution} LINQ to XML (formerly XLinq) { Queries performed against the XML source}
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38. SQL ISNULL function LINQ 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 { u.Id, FirstName= u.FirstName, LastName= u.LastName, UserId= m.UserId, MobileNo= m.MobileNo?? "N/A" };
39. Selectmany Flattens the resulting sequences into one sequence users.SelectMany ( u => u.Userclient, (u, uc) => new { Name = u.Name, Mobileno = uc.Mobile, IsApproved = uc.IsApproved } )
40. Linq Deferred Execution Query not going to execute at this point var query = from customer in db.Customers where customer.City == "Paris" select customer; Query get executed when we apply method like (Count(),toList() etc.) int count = (from customer in db.Customers where customer.City == "Paris" select customer).Count();