Linq intro

3,167 views

Published on

Bài giảng về LINQ

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

No Downloads
Views
Total views
3,167
On SlideShare
0
From Embeds
0
Number of Embeds
61
Actions
Shares
0
Downloads
285
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Linq intro

  1. 1. Language Integrated Query (LINQ) Nguyen Minh Dao [email_address]
  2. 2. Introduction We use many different types of query SQL, XQuery/XPath, DataView row filters, etc. Maybe we could enhance productivity by... Deciding on one query expression syntax Enabling compilers to check queries & results Allow extensibility to target all kinds of data
  3. 3. Agenda C#3 and VB9 Language Enhancements Building to LINQ to Objects LINQ to XML LINQ to SQL LINQ to DataSets
  4. 4. 2006 2007 2008 .NET Framework - VS Roadmap “ Rosario” 3.0 RTM 3.5 RTM VS Extensions for WF VS Extensions for WCF/WPF CTP ASP.NET AJAX 1.0 SQL Server 2008 ADO.NET Entity Framework VS 2008 Beta 2 .NET Framework 3.5 Beta 2
  5. 5. What is the .NET Framework 3.5? .NET Framework 2.0 + SP1 Windows Presentation Foundation Windows Communication Foundation Windows Workflow Foundation Windows CardSpace .NET Framework 3.0 + SP1 .NET Framework 3.5 LINQ ASP.NET 3.5 CLR Add-in Framework Additional Enhancements
  6. 6. Multi-targeting in Visual Studio 2008
  7. 7. Compiler Features Most are LINQ enablers VB9 XML Literals Relaxed Delegates C# 3.0 Extension Methods Object Initialisers Anonymous Types Local Type Inference Lambda expressions Collection Initializers Partial Methods Automatic Properties If Ternary Operator Nullable Syntax Lambda statements
  8. 8. Language Innovations var contacts = from c in customers where c.City == "Hove" select new { c.Name, c.Phone }; var contacts = customers .Where(c => c.City == "Hove") .Select(c => new { c.Name, c.Phone }); Extension methods Lambda expressions Query expressions Object initializers Anonymous types Local variable type inference
  9. 9. Extension methods Add methods to existing types “virtually” Static method with first “ this ” parameter Scoping using namespaces string name = “Bart” ; string reversed = name.Reverse(); static class MyExtensions { public static string Reverse( this string s) { char [] c = s.ToCharArray(); Array .Reverse(c); return new string (c); } } Promoted first parameter string name = “Bart” ; string reversed = MyExtensions .Reverse(name); static class MyExtensions { public static string Reverse( string s) { char [] c = s.ToCharArray(); Array .Reverse(c); return new string (c); } }
  10. 10. Automatic properties Tired of writing properties? Compiler can generate the get/set plumbing class Customer { private string _name; public string Name { get { return _name; }; set { _name = value ; }; } } class Customer { public string Name { get ; set ; } } Setter required; can be private or internal
  11. 11. Object initializers Ever found the constructor of your taste? Insufficient overloads, so pick one Call various property setters class Customer { public string Name { get ; set ; } public int Age { get ; set ; } } Customer c = new Customer (); c.Name = “Bart” ; c.Age = 24; var c = new Customer (){ Name = “Bart” , Age = 24}; Can be combined with any constructor call
  12. 12. Collection initializers Arrays easier to initialize than collections?! int [] ints = new int [] { 1, 2, 3 }; List < int > lst = new List < int >(); lst.Add(1); lst.Add(2); lst.Add(3); int [] ints = new int [] { 1, 2, 3 }; var lst = new List < int >() { 1, 2, 3 }; Works for any ICollection class by calling its Add method
  13. 13. Anonymous types Let the compiler cook up a type Can’t be returned from a method Local variable type inference becomes a must Used in LINQ query projections var person = new { Name = “Bart” , Age = 24 }; var customer = new { Id = id, person.Name };
  14. 14. Lambda expressions Functional-style anonymous methods delegate R BinOp <A,B,R>(A a, B b); int Calc( BinOp < int , int , int > f, int a, int b) { return f(a, b) } int result = Calc( delegate ( int a, int b) { return a + b; }, 1, 2); var result = Calc((a, b) => a + b, 1, 2); Parameter types inferred based on the target delegate
  15. 15. Demo Anonymous types, automatic properties, collection initializers, extension methods
  16. 16. First, A Taste of LINQ using System; using System.Query; using System.Collections.Generic;   class app { static void Main() { string [] names = { "Burke", "Connor", "Frank", "Everett", "Albert", "George", "Harris", "David" };   var expr = from s in names where s.Length == 5 orderby s select s.ToUpper();   foreach ( string item in expr) Console .WriteLine(item); } } BURKE DAVID FRANK
  17. 17. Query Expressions Introduce SQL-Like Syntax to Language Compiled to Traditional C# (via Extension Methods) from itemName in srcExpr join itemName in srcExpr on keyExpr equals keyExpr (into itemName )? let itemName = selExpr where predExpr orderby ( keyExpr (ascending | descending)?)* select selExpr group selExpr by keyExpr   into itemName query-body
  18. 18. LINQ Architecture LINQ-enabled data sources LINQ To Objects LINQ To XML LINQ-enabled ADO.NET Visual Basic Others LINQ To Entities LINQ To SQL LINQ To Datasets .Net Language Integrated Query (LINQ) Visual C# Objects Databases
  19. 19. LINQ Architecture LINQ-enabled data sources LINQ To Objects LINQ To XML LINQ-enabled ADO.NET Visual Basic Others LINQ To Entities LINQ To SQL LINQ To Datasets .Net Language Integrated Query (LINQ) Visual C# Objects Databases
  20. 20. LINQ to Objects Native query syntax in C# and VB IntelliSense Autocompletion Query Operators can be used against any .NET collection (IEnumerable<T>) Select, Where, GroupBy, Join, etc. Deferred Query Evaluation Lambda Expressions using System; using System.Query; using System.Collections.Generic;   class app { static void Main() { string [] names = { "Burke", "Connor", "Frank", "Everett", "Albert", "George", "Harris", "David" };   Func < string , bool > filter = s => s.Length == 5; Func < string , string > extract = s => s; Func < string , string > project = s = s.ToUpper();   IEnumerable < string > expr = names .Where(filter) .OrderBy(extract) .Select(project);   foreach ( string item in expr) Console .WriteLine(item); } } BURKE DAVID FRANK using System; using System.Query; using System.Collections.Generic;   class app { static void Main() { string [] names = { "Allen", "Arthur", "Bennett" };   IEnumerable < string > ayes = names .Where(s => s[0] == 'A');   foreach ( string item in ayes) Console .WriteLine(item);   names[0] = "Bob";   foreach ( string item in ayes) Console .WriteLine(item); } } Arthur using System; using System.Query; using System.Collections.Generic;   class app { static void Main() { string [] names = { "Allen", "Arthur", "Bennett" };   IEnumerable < string > ayes = names .Where(s => s[0] == 'A');   foreach ( string item in ayes) Console .WriteLine(item);   names[0] = "Bob";   foreach ( string item in ayes) Console .WriteLine(item); } } Allen Arthur using System; using System.Query; using System.Collections.Generic;   class app { static void Main() { string [] names = { "Burke", "Connor", "Frank", "Everett", "Albert", "George", "Harris", "David" };   IEnumerable < string > expr = from s in names where s.Length == 5 orderby s select s.ToUpper();   foreach ( string item in expr) Console .WriteLine(item); } } BURKE DAVID FRANK
  21. 21. LINQ Architecture LINQ-enabled data sources LINQ To Objects LINQ To XML LINQ-enabled ADO.NET Visual Basic Others LINQ To Entities LINQ To SQL LINQ To Datasets .Net Language Integrated Query (LINQ) Visual C# Objects Databases
  22. 22. LINQ to SQL Overview
  23. 23. LINQ to SQL Architecture Enumerate SQL Query or SProc Rows Objects SubmitChanges() DML or SProcs Application LINQ to SQL from c in db.Customers where c.City == "London" select c.CompanyName SELECT CompanyName FROM Customer WHERE City = 'London' db.Customers.Add(c1); c2.City = “Seattle"; db.Customers.Remove(c3); INSERT INTO Customer … UPDATE Customer … DELETE FROM Customer …
  24. 24. LINQ to SQL DataContext is the central class Use code-gen for ORM SQL is only submitted when needed Parent-child relationships are respected Control of deferred loading Can insert/update/delete Transactionally, with concurrency checks
  25. 25. LINQ to DataSet Query expressions over in-memory data Works with untyped or typed DataSets If query returns some kind of DataRow: - Can yield results as a DataView ...and therefore databind to those results
  26. 26. Demo LINQ to Objects LINQ to SQL
  27. 27. LINQ Architecture LINQ-enabled data sources LINQ To Objects LINQ To XML LINQ-enabled ADO.NET Visual Basic Others LINQ To Entities LINQ To SQL LINQ To Datasets .Net Language Integrated Query (LINQ) Visual C# Objects Databases
  28. 28. LINQ to XML Creating XML Constructors lend themselves to nesting Can use LINQ (over anything) to build XML Querying Use normal axes from XML infoset Get full power of query expressions over XML Select, where, group by, etc. Xml Namespaces
  29. 29. Demo LINQ to XML
  30. 30. That’s LINQ A combination of new language features, and new fx3.5 classes (with extension methods) A common query expression syntax Freedom to implement across different kinds of data It’s TYPED... The compiler can check your queries The compiler can check your results
  31. 31. MSDN in the UK Visit http://msdn.co.uk Newsletter Events Nugget Videos Blogs
  32. 32. THANK YOU

×