LINQ Architecture: SQL-like
LINQ Architecture: SQL-like
LINQ Architecture
LINQ
Gang Luo
Xuting Zhao
OO-style:
OO style: Executable function
Z Anonymous functional. Can be assigned to a delegate variable.
names.Where(s => s.Length==5)
Z No need to indicate the types
.OrderBy(s => s) Z Can be passed to methods as parameters.
.Select(s => s.ToUpper()); Expression Tree
Z Efficient in-memory data representations of lambda expressions
Where, OrderBy, and Select are operators. Z Changing the behaviors of the expressions
The arguments to these operators are Z Applying your own optimization
Lambda Expression.
Z 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.
1
LINQ Operations (cont.) LINQ Operations (cont.)
Select Many
Group Join Z Each object in the result set may contain a collection or array
Z Select many help decompose the structure and flatten the result
ZThe lambda expression for shaping is applied
on the outer element and the set of all the inner
elements that matches the outer one. Z All the elements could be traversed in one foreach loop.
Aggregation
ZShape the result at a set level
Z Standard aggregation operators.
Min, Max, Sum, Average.
Int totalLength=names.Sum(s => s.Length);
Z General purpose (generic) operator.
2
LINQ to SQL Consistency
Data Model Every object will be tracked by LINQ the moment it
is loaded from database.
Concurrency Transaction/Update
Optimistic concurrency When update, first check whether new object is added (by
tracking mechanism) if yes, insert statement will be
Conflict checking when SubmitChanges() is called generated. What does Django do here?
By default, transaction will abort and an exception
Modification will not hit the database until the
will be thrown when a conflict is detected. SubmitChanges() method is called
User can handle the conflict in the exception catch
All operations will be translated into SQL statements
block.
User can set whether or not to detect the conflict All modifications will be encapsulated into a transaction.
when one column get updated.
3
LINQ to XML Performance
LINQ to XML LINQ has more control and efficiency in O/R
Mapping than NHibernate
Z LINQ: Externl Mapping or Attribute Mapping
Z NHibernate:
NHib t E Externl
t lM Mapping
i
XML to LINQ
Because of mapping, LINQ is lower than database
tools such as SqlDataReader or SqlDataAdapter
Z In large dataset, their performance are more and more
similar
Thanks!