0% found this document useful (0 votes)
54 views4 pages

LINQ Architecture: SQL-like

This document provides an introduction and overview of LINQ (Language Integrated Query). It discusses LINQ query expressions, lambda expressions, LINQ operations like Join and SelectMany, deferred execution with IQueryable, consistency and concurrency handling, and using LINQ to access and manipulate XML and relational databases. Key topics covered include LINQ architecture, different query styles, expression trees, controlling query execution, handling relationships between data sources, and managing transactions and updates.

Uploaded by

lordzang
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views4 pages

LINQ Architecture: SQL-like

This document provides an introduction and overview of LINQ (Language Integrated Query). It discusses LINQ query expressions, lambda expressions, LINQ operations like Join and SelectMany, deferred execution with IQueryable, consistency and concurrency handling, and using LINQ to access and manipulate XML and relational databases. Key topics covered include LINQ architecture, different query styles, expression trees, controlling query execution, handling relationships between data sources, and managing transactions and updates.

Uploaded by

lordzang
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Introduction

™ LINQ Architecture

LINQ

Gang Luo
Xuting Zhao

Query Expression Lambda Expressions


™ SQL-like:
from s in names ™ Examples:
where s.Length == 5 orderby sselect s.ToUpper(); Z s => s.Length == 5

™ 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.

Methods Extension LINQ Operations


You can control not only by Lambda Expression, ™ Join
but also by methods extension Z When there is relationship (e.g. foreign key) between two tables, no
explicit join operation is needed
Z Using dot notation to access the relationship properties, and
navigate all the matching objects.

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.

Spotlight Spotlight (cont.)


™ IQueryable<T> interface will defer the evaluation of the query. ™ Nested defer

™ An expression tree will represent all the deferred queries as a whole.

™ Several operations could be “merged”,


only one SQL query will be generated
and sent to database (Similar to Django)
What if you want the intermediate result?
™ Multi-level defer

Spotlight (cont.) Spotlight (cont.)


™ Defer Execution ™ Object of new type could be generated on the fly without first define it.
This is useful for projection to select one or more fields of another
ZAdvantages structure.
™Performance!
™ The type
yp will be dynamically
y yggenerated with setters and g
getters to
™Query
Q d
dependency!
d ! corresponding members. Some common methods is also provided.
ZDisadvantages
™ No other methods will be added to this type.
™Divide one query into multiple ones
But that is already enough!
™You iterate the result set 100 times,
the query will be executed 100 ™ The object is created and initialized by
times. Anonymous Object Initializer.
™Users have to be very careful

2
LINQ to SQL Consistency
™ Data Model ™ Every object will be tracked by LINQ the moment it
is loaded from database.

™ 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.
™ LINQ to SQL helps connect to relational and manipulate the relational
data as objects in memory. It achieves this by translating the
operations into SQL statements.
™ When an object is deleted, it could still exist in
memory, but it will not cause inconsistency.

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.

Transaction/Update (cont.) LINQ to XML


™ If an exception is throw during the update, all the changes ™ Ddd
will be rolled back

™ One SubmitChanges() is actually one transaction.


transaction (pros
and cons?)

™ Users can also explicitly indicate a new transaction scope.

™ The LINQ to XML Class Hierarchy


™ http://msdn.microsoft.com/en-us/library/bb308960.aspx

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!

You might also like