Linq and C# 3.0 docx

58 424 1
Linq and C# 3.0 docx

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

28 JUNI 2006 | © CLASS -A A new way and language to query data 2 28 JUNI 2006 | © CLASS -A Overview • What is Linq? • Flavors of Linq • Linq to XML • Linq to DataSets • Linq to SQL • Linq under the covers • Linq deferred • Q&A and/or Discussion 3 28 JUNI 2006 | © CLASS -A Introducing… • Alex Thissen • Trainer/coach • Weblog at http://www.alexthissen.nl • INETA • Next step in user group evolution • By and for user group community • Class-A • Knowledge provider • Training and coaching on Microsoft development • www.class - a.nl 4 28 JUNI 2006 | © CLASS -A What is Linq? • Linq stands for Language Integrated Query • New set of keywords in C# 3.0 and VB.NET 9.0 to express queries over data IEnumerable <Match> homeMatchesWon = from m in matches where m.GoalsHome > m.GoalsAway order by m.HomeTeam , m.AwayTeam descending select m; Dim homeMatchesWon As IEnumerable(Of Match) = From m In matches Where m.GoalsHome > m.GoalsAway Order By m.HomeTeam , m.AwayTeam Descending Select m 5 28 JUNI 2006 | © CLASS -A Linq Linq project Languages Linq to DataSets Standard Query Operators C# 3.0 VB 9.0 Other Linq to Entities Linq to XML Linq to Objects Linq to SQL 6 28 JUNI 2006 | © CLASS -A Query expressions • Queries expressed with language keywords • Special syntax • Query expressions are translated into invocations of methods from id in source [ join id in source on expr equals expr ] [ let id = expr ] [ where e xpr ] [ orderby ordering, ordering, … ] select expr | group expr by key [ into id ] 7 28 JUNI 2006 | © CLASS -A Standard Query Operators • Known set of methods to express queries • Larger set than is integrated in language • An other programming model for queries • Also called explicit dot notation • Standard Query Operators are provided by extension methods • on any object that implements IEnumerable <T> 8 28 JUNI 2006 | © CLASS -A Explicit dot notation example IEnumerable <Team> winningHomeTeams = matches . Where(m => m.GoalsHome > m.GoalsAway ) . OrderBy(m => m.HomeTeam.Name ) . Select(m => m.HomeTeam ); 9 28 JUNI 2006 | © CLASS -A Standard Query Operators GroupBy GroupBy Grouping Grouping Any, All Any, All Quantifiers Quantifiers ToArray ToArray , , ToList ToList , , ToDictionary ToDictionary Conversion Conversion Count, Sum, Min, Max, Average Count, Sum, Min, Max, Average Aggregation Aggregation First, First, FirstOrDefault FirstOrDefault , , ElementAt ElementAt Elements Elements Cast, Cast, OfType OfType Casting Casting Distinct, Union, Intersect, Except Distinct, Union, Intersect, Except Sets Sets Take, Skip, Take, Skip, TakeWhile TakeWhile , , SkipWhile SkipWhile Partitioning Partitioning OrderBy OrderBy , , ThenBy ThenBy Ordering Ordering Select, Select, SelectMany SelectMany Projection Projection Where Where Restriction Restriction 10 28 JUNI 2006 | © CLASS -A Demo: Linq fundamentals • Linq queries • Standard query operators • Explicit dot notation [...]... between CLR and database world 1 Attributes on CLR types 2 XML mapping file • • 28 JUNI 2006 | © CLASS-A Table class handles mapping and materialization of objects into context S qlMetal.exe tool and DLinq designer help in generation of both mapping types 23 Attribute-based mappings [System.Data.DLinq.Table(Name="Blogs")] public partial class Blog { private string _Name; [System.Data.DLinq.Column(Name="Name",... ystem.Data.DLinq.INotifyPropertyChanging • S ystem.ComponentModel.INotifyPropertyChanged • Allows collections and referenced objects to be kept up to date 28 JUNI 2006 | © CLASS-A 26 Future of Linq to S QL • Linq to S competes in O/R mapping space QL with ADO.NE E T ntities • ADO.NE 3.0 will introduce E T ntities • Mapping from conceptual to logical model • More flexible and powerful mapping approach • Will Linq. .. types: XNode 13 E xtra Linq to XML query operators • Defined as extension methods on IEnumerable in XElementSequence • Operators for XPath axis: • • Ancestors, SelfAndAncestors, Descendants, SelfAndDescendants, ElementsBefore/AfterThis, NodesBefore/AfterThis Operators for node sets: • Nodes, E lements, Attributes • XPath axis sets: Nodes, DescendantNodes, AncestorNodes, SelfAndDescendantNodes... returned from Linq to S queries are QL materialized into DataContext • DataContext class handles • Change tracking • Object identity • Currently load context of objects can be saved, discarded or accepted • 28 JUNI 2006 | © CLASS-A E ntityS et uses lazy loading of related collections into context 25 Notifying changes • UI and object graphs have to keep in sync with changes, inserts and deletes •... created DataTable 18 E xtra Linq to DataS query operators ets • DataTable • Work with sets of DataRows • • DistinctRows, EqualAllRows, ExceptRows, IntersectRows, UnionRows DataRow • Adds strong typing and null support • Field reads from fields • SetField sets values on fields 28 JUNI 2006 | © CLASS-A 19 Queries and object/relational mapping 28 JUNI 2006 | © CLASS-A Linq to S for relational data... technology • Translates Linq queries to S statements QL • Builds on ADO.NE and NE Transactions T T • Persistence services • Automatic change tracking of objects • Updates through S statements or stored QL procedures 28 JUNI 2006 | © CLASS-A 21 Mapping strategy Blog table ID Data context Blog object Name Title Posting table ID Body Posted Posting objects • • 1 to 1 correspondence between type and table • 28... SelfAndDescendantNodes 28 JUNI 2006 | © CLASS-A 14 More Linq to XML • Add annotations to the XContainer nodes (XElement and XDocument) • Do not show up in XML output • XStreamingElement defers generation of XML element content • Use in place of XElement • Save method triggers creation • Lazy/streaming output of XML to file or writer 28 JUNI 2006 | © CLASS-A 15 Demo: Linq to XML • • Creating XML • 28 JUNI 2006... JUNI 2006 | © CLASS-A 27 How Linq works 28 JUNI 2006 | © CLASS-A Working with sources of data • Query operators work on sets of data • from m in matches • E verything that implements IEnumerable can be queried • Implementing IEnumerable seems to get a class new methods • Past the syntactic sugar it is all about S tandard Query Operators 28 JUNI 2006 | © CLASS-A 29 Linq under the covers • Query... => m.HomeTeam.Name) Select(m => m.HomeTeam); 28 JUNI 2006 | © CLASS-A 30 C# 3.0: E xtension methods • Project instance methods onto existing classes • Declared in static class with static methods • Introducing another this keyword • Type of first argument defines class to extend • Compiler emits [ExtensionAttribute] to class and extension methods • Import namespace to bring extension methods into scope... > 0); 28 JUNI 2006 | © CLASS-A 32 Back to IE numerable • S tandard Query Operators are available for all IEnumerable implementing types • E xtension methods are in class S ystem.Query.S equence • Implementation of S equence relies heavily on iterators and yield keyword 28 JUNI 2006 | © CLASS-A 33 Iterators • Perform iterations in C# with foreach • Iterator pattern by implementation of IEnumerable . JUNI 200 6 | © CLASS -A Linq Linq project Languages Linq to DataSets Standard Query Operators C# 3. 0 VB 9 .0 Other Linq to Entities Linq to XML Linq to. JUNI 200 6 | © CLASS -A A new way and language to query data 2 28 JUNI 200 6 | © CLASS -A Overview • What is Linq? • Flavors of Linq • Linq to XML • Linq

Ngày đăng: 14/03/2014, 20:20

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan