LINQ Slides from Gang Luo Xuting Zhao and

  • Slides: 38
Download presentation
LINQ Slides from Gang Luo, Xuting Zhao and Damien Guard

LINQ Slides from Gang Luo, Xuting Zhao and Damien Guard

What is LINQ? Language Integrated Query l Make query a part of the language

What is LINQ? Language Integrated Query l Make query a part of the language l Component of. NET Framework 3. 5 l

Query without LINQ l Objects using loops and conditions foreach (Customer c in customers)

Query without LINQ l Objects using loops and conditions foreach (Customer c in customers) if (c. Region == "UK"). . . l Databases using SQL SELECT * FROM Customers WHERE Region='UK' l XML using XPath/XQuery //Customers/Customer[@Region='UK']

ADO without LINQ Sql. Connection con = new Sql. Connection(. . . ); con.

ADO without LINQ Sql. Connection con = new Sql. Connection(. . . ); con. Open(); Sql. Command cmd = new Sql. Command( @"SELECT * FROM Customers WHERE c. Region = @Region", con ); cmd. Parameters. Add. With. Value("@Region", "UK"); Data. Reader dr = cmd. Execute. Reader(); while (dr. Read()) { string name = dr. Get. String(dr. Get. Ordinal("Name")); string phone = dr. Get. String(dr. Get. Ordinal("Phone")); Date. Time date = dr. Get. Date. Time(3); } dr. Close(); con. Close();

Query with LINQ C# var my. Customers = from c in customers where c.

Query with LINQ C# var my. Customers = from c in customers where c. Region == "UK" select c;

More LINQ queries var good. Custs = (from c in db. Customers where c.

More LINQ queries var good. Custs = (from c in db. Customers where c. Post. Code. Starts. With("GY") orderby c. Sales descending select c). Skip(10). Take(10);

Local variable type inference Compiler can infer types from initializer assignments l var keyword

Local variable type inference Compiler can infer types from initializer assignments l var keyword indicates compiler inferred type l § Still strong-typed § This is not like Java. Script var a = 10; // Simple types var x = new { Blog = “attardi”, Created = Date. Time. Now }; // Anonymous types l Essential when using anonymous types

Anonymous Types l Object of new type generated on the fly without first defining

Anonymous Types l Object of new type generated on the fly without first defining it. Useful for projection to select one or more fields of another structure. l The type will be dynamically generated with setters and getters to corresponding members. Some common methods are also provided. l No other methods will be added to this type. But that is already enough! l The object is created and initialized by Anonymous Object Initializer.

Advantages l Unified data access Single syntax to learn and remember l Strongly typed

Advantages l Unified data access Single syntax to learn and remember l Strongly typed Catch errors during compilation l Intelli. Sense Prompt for syntax and attributes l Bindable result sets

Architecture

Architecture

LINQ to Objects int[] nums = new int[] {0, 4, 2, 6, 3, 8,

LINQ to Objects int[] nums = new int[] {0, 4, 2, 6, 3, 8, 3, 1}; double average = nums. Take(6). Average(); var above = from n in nums where n > average select n;

LINQ to Objects l Query any IEnumerable<T> source Includes arrays, List<T>, Dictionary. . .

LINQ to Objects l Query any IEnumerable<T> source Includes arrays, List<T>, Dictionary. . . l Many useful operators available Sum, Max, Min, Distinct, Intersect, Union l Expose your own data with IEnumerable<T> or IQueryable<T> l Create operators using extension methods

LINQ operators Aggregate Conversion Aggregate Average Count Max Min Sum Ordering Partitioning Cast Order.

LINQ operators Aggregate Conversion Aggregate Average Count Max Min Sum Ordering Partitioning Cast Order. By Of. Type Then. By To. Array Descending To. Dictionary Reverse To. List To. Lookup To. Sequence and others … Skip. While Take. While Sets Concat Distinct Except Intersect Union

Query Expression l SQL-like: from s in names where s. Length == 5 orderby

Query Expression l SQL-like: from s in names where s. Length == 5 orderby select s. To. Upper(); l OO-style: names. Where(s => s. Length==5) . Order. By(s => s). Select(s => s. To. Upper()); l Where, Order. By, and Select are operators. The arguments to these operators are Lambda Expression.

 Lambda Expressions l Examples: § s => s. Length == 5 l Executable

Lambda Expressions l Examples: § s => s. Length == 5 l Executable function § Anonymous functional. Can be assigned to a delegate variable. § No need to indicate the types § Can be passed to methods as parameters. l Expression Tree § Efficient in-memory data representations of lambda expressions § Changing the behaviors of the expressions § Applying your own optimization

Function Types l Func<int, bool> is a shorthand for public delegate bool Func(int a

Function Types l Func<int, bool> is a shorthand for public delegate bool Func(int a 0); // Initialized with anonymous method Func<int, bool> even= delegate (int x) { return x % 2 == 0; }; // Initialized with lambda expression Func<int, bool> even 2 = x => x % 2 == 0;

Methods Extension l Control not only by Lambda Expression, but also by methods extension

Methods Extension l Control not only by Lambda Expression, but also by methods extension public static class Enumerable { public static IEnumerable<T> Where<T>( this IEnumerable<T> source, Func<T, bool> predicate) { foreach (T item in source) if (predicate(item)) yield return item; } }

LINQ Operations l Join § When there is relationship (e. g. foreign key) between

LINQ Operations l Join § When there is relationship (e. g. foreign key) between two tables, no explicit join operation is needed § Using dot notation to access the relationship properties, and navigate all the matching objects. var q = from o in db. Orders where o. Customer. City == “London” select o; § To join any two data sources on any attribute, you need an explicit join operation. var query = names. Join(people, n => n, p => p. Name, (n, p) => p); The lambda expression for shaping (n, p) => p will be applied on each matching pairs.

LINQ Operations (cont. ) l Group Join § The lambda expression for shaping is

LINQ Operations (cont. ) l Group Join § The lambda expression for shaping is applied on the outer element and the set of all the inner elements that matches the outer one. § Shape the result at a set level var query = names. Group. Join(people, n => n, p => p. Name, (n, matching) => new { Name = n, Count = matching. Count() } )

LINQ Operations (cont. ) l Select Many § Each object in the result set

LINQ Operations (cont. ) l Select Many § Each object in the result set may contain a collection or array § Select many help decompose the structure and flatten the result var query = names. Select. Many(n => people. Where(p => n == p. Name)) § All the elements could be traversed in one foreach loop. l Aggregation § Standard aggregation operators: Min, Max, Sum, Average. int total. Length=names. Sum(s => s. Length); § General purpose (generic) operator: static U Aggregate<T, U>(this IEnumerable<T> source, U seed, Func<U, T, U> func)

LINQ Deferred l A LINQ data source can actually implement one of two interfaces:

LINQ Deferred l A LINQ data source can actually implement one of two interfaces: § IEnumerable<T> § IQueryable<T> public interface IQueryable<T> : IEnumerable<T>, IQueryable, IEnumerable { IQueryable<S> Create. Query<S>(Expression exp); S Execute<S>(Expression exp); } l Create deferred query execution plan

IQueryable l IQueryable<T> interface will defer the evaluation of the query. l An expression

IQueryable l IQueryable<T> interface will defer the evaluation of the query. l An expression tree will represent all the deferred queries as a whole. l Several operations could be “merged”, only one SQL query will be generated and sent to database. l Multi-level defer

Lambda Expressions Revisited l Lambda expressionscan represent either IL code or data § Expression<T>

Lambda Expressions Revisited l Lambda expressionscan represent either IL code or data § Expression<T> makes the difference Func<int, bool> lambda. IL = n => n % 2 == 0; Expression<Func<int, bool>> lambda. Tree = n => n % 2 == 0; l Compiler handles Expression<T> types differently § Emits code to generate expression tree instead of usual IL for delegate

Expression Trees Expression tree are hierarchical trees of instructions that compose an expression l

Expression Trees Expression tree are hierarchical trees of instructions that compose an expression l Add value of expression trees l § Actual creating of IL is deferred until execution of query § Implementation of IL creation can vary l Trees can even be remoted for parallel processing

Creating IL from Expression Trees l Right before execution tree is compiled into IL

Creating IL from Expression Trees l Right before execution tree is compiled into IL Expression<Func<Posting, bool>> predicate = p => p. Posted < Date. Time. Now. Add. Days(-5); Func<Posting, bool> d = predicate. Compile(); l Implementation of IL generation differs very much for each Linq flavor. § Linq to SQL generates IL that runs SQL commands § Linq to Objects builds IL with Sequence extensions methods

Nested defer l Nested defer var q = from c in db. Customers where

Nested defer l Nested defer var q = from c in db. Customers where c. City == “London” select new { c. Contact. Name, c. Phone } var q 2 = from c in q. As. Enumerable() select new { Name = Do. Name. Processing(c. Contact. Name), Phone = Do. Phone. Processing(C. Phone) }; l What if you want the intermediate result? string last. Name = “Simpson” var persons = from p in person. List where p. Last. Name = last. Name select p; last. Name = “Flanders” foreach (Person p in persons) Console. Write. Line(“{0} {1}”, p. First. Name, p. Last. Name);

Deferred Execution l Advantages § Performance! § Query dependency! l Disadvantages § Divide one

Deferred Execution l Advantages § Performance! § Query dependency! l Disadvantages § Divide one query into multiple ones § If you iterate over the result set 100 times, the query will be executed 100 times. § Users have to be very careful

LINQ to SQL l Data Model [Table(Name=“Customers”)] public class Customer { [Column(Id=true)] public string

LINQ to SQL l Data Model [Table(Name=“Customers”)] public class Customer { [Column(Id=true)] public string Customer. ID; … private Entity. Set<Order> _Orders; [Association(Storage=“_Orders”, Other. Key=“Customer. ID”)] public Entity. Set<Order> Orders { get { return _Orders; } set { _Orders. Assign(value); } } } l LINQ to SQL helps connect to relational and manipulate the relational data as objects in memory. l It achieves this by translating the operations into SQL statements.

LINQ to SQL l l l Object-relational mapping Records become strongly-typed objects Data context

LINQ to SQL l l l Object-relational mapping Records become strongly-typed objects Data context is the controller mechanism Facilitates update, delete & insert Translates LINQ queries behind the scenes Type, parameter and injection safe

Database mapping l l l Map tables & fields to classes & properties Generates

Database mapping l l l Map tables & fields to classes & properties Generates partial classes with attributes Each record becomes an object Data context represents the database Utilize tables, views or stored procedures

Modifying objects Update Set object properties l Delete l context. Table. Delete. On. Submit(object)

Modifying objects Update Set object properties l Delete l context. Table. Delete. On. Submit(object) l Insert context. Table. Insert. On. Submit(object) l Commit changes back context. Submit. Changes() Transactional - all or nothing

Consistency l Every object will be tracked by LINQ the moment it is loaded

Consistency l Every object will be tracked by LINQ the moment it is loaded from database. l The tracking mechanism monitor the manipulation on relationship properties. Once you modify one side of the relationship, LINQ will modify the other to keep it consistent. l When an object is deleted, it could still exist in memory, but it will not cause inconsistency.

Concurrency l l l Optimistic concurrency Conflict checking when Submit. Changes() is called By

Concurrency l l l Optimistic concurrency Conflict checking when Submit. Changes() is called By default, transaction will abort and an exception will be thrown when a conflict is detected. User can handle the conflict in the exception catch block. User can set whether or not to detect the conflict when one column get updated.

Transaction/Update l When update, first check whether new object is added (by tracking mechanism)

Transaction/Update l When update, first check whether new object is added (by tracking mechanism) if yes, insert statement will be generated. What does Django do here? l Modification will not hit the database until the Submit. Changes() method is called l All operations will be translated into SQL statements l All modifications will be encapsulated into a transaction.

Transaction/Update (cont. ) l If an exception is throw during the update, all the

Transaction/Update (cont. ) l If an exception is throw during the update, all the changes will be rolled back l One Submit. Changes() is actually one transaction. (pros and cons? ) l Users can also explicitly indicate a new transaction scope.

LINQ to XML Class Hierarchy http: //msdn. microsoft. com/en-us/library/bb 308960. aspx

LINQ to XML Class Hierarchy http: //msdn. microsoft. com/en-us/library/bb 308960. aspx

LINQ to XML l LINQ to XML var query = from p in people

LINQ to XML l LINQ to XML var query = from p in people where p. Can. Code select new XElement(“Person”, new XAttribute(“Age”, p. Age), p. Name); l XML to LINQ var x = new XElement(“People”, from p in people where p. Can. Code select new XElement(“Person”, new XAttribute(“Age”, p. Age), p. Name);

Performance l LINQ has more control and efficiency in O/R Mapping than NHibernate §

Performance l LINQ has more control and efficiency in O/R Mapping than NHibernate § LINQ: Externl Mapping or Attribute Mapping § NHibernate: Externl Mapping l Because of mapping, LINQ is slower than database tools such as Sql. Data. Reader or Sql. Data. Adapter § In large dataset, their performance are more and more similar