Beruflich Dokumente
Kultur Dokumente
MAHTAB HAIDER
18th April 2014
Agenda
What is LINQ
Transforming Data With LINQ
ADO.NET Entity Framework
LINQ and ADO.NET
LINQ to SQL
LINQ to Entities
-2-
Document Name
CONFIDENTIAL
Document Name
CONFIDENTIAL
Introduction to LINQ:
Innovation introduced in Visual Studio 2008 and .NET Framework
version 3.5 that bridges the gap between the world of objects and
the world of data.
-5-
Document Name
CONFIDENTIAL
LINQ
Architecture
C#
OTHERS
VB.NET
LINQ
to Datasets
LINQ
to SQL
-6-
LINQ
to Entities
LINQ
to XML
Document Name
CONFIDENTIAL
Document Name
CONFIDENTIAL
-8-
Document Name
CONFIDENTIAL
-9-
Document Name
CONFIDENTIAL
- 10 -
Document Name
CONFIDENTIAL
Document Name
CONFIDENTIAL
LINQ Innovations
var contacts =
from c in customers
where c.City == "Hove"
select new { c.Name, c.Phone };
Query
expressions
Local variable
type
inference
Lambda
Lambda
var contacts =
expressions
customers
.Where(c => c.City == "Hove")
.Select(c => new { c.Name, c.Phone });
Extension
methods
Object
initializers
Anonymous
types
- 12 -
Document Name
CONFIDENTIAL
Determine type at
run time
Document Name
CONFIDENTIAL
- 15 -
Document Name
CONFIDENTIAL
- 16 -
Formatting the
output
Document Name
CONFIDENTIAL
- 17 -
Document Name
CONFIDENTIAL
- 18 -
Document Name
CONFIDENTIAL
ADO.NET Evolution
LINQ
to
SQL
ADO.NET
Entity
Data
Framework
Services
Azure
RIA
Table
Services
Services
Document Name
CONFIDENTIAL
- 21 -
Document Name
CONFIDENTIAL
- 23 -
Document Name
CONFIDENTIAL
Object
Services
Object Services
Object Query
Entity SQL
Entity SQL
Entity
Client
Entity Client
Entity Framework Layer
Conceptual
CSDL
EDM
Mapping
MSL
Core of EF
Logical Storage
SSDL
- 24 -
Document Name
CONFIDENTIAL
pi
Map
Mappi
ng
ng
Programmi
ng
Model
Rob Vettor
Reader
Connection
Adapter
Command
Store
25
Document Name
CONFIDENTIAL
The EDM
Set of objects that describe structure of your
business data and
map to your underlying data store
Contained in Three XML sections stored in [Filename].edmx file:
Database
Schema
Storage Model
Entity
Objects
Mapping
UI
Database
Rob Vettor
Conceptual Model
- 26 -
OO
Services
26
Document Name
CONFIDENTIAL
- 27 -
Document Name
CONFIDENTIAL
- 28 -
Document Name
CONFIDENTIAL
- 29 -
Document Name
CONFIDENTIAL
LINQ to ADO.NET
Enables us to query over any enumerable object in ADO.NET
Three separate LINQ to ADO.NET technologies:
LINQ to DataSet:
provides richer, optimized querying over the DataSet
LINQ to SQL:
enables you to directly query SQL Server database schemas
LINQ to Entities:
allows you to query an Entity Data Model.
- 31 -
Document Name
CONFIDENTIAL
LINQ to SQL
LINQ to SQL
Component of.NET Frameworkversion 3.5 that provides a runtime infrastructure for managing relational data as objects.
Data model of arelational database is mapped to an object
model expressed in programming language.
When the application runs:
LINQ to SQL translates the language-integrated queries in the
object model into SQL
Sends them to the database for execution.
When the database returns the results:
LINQ to SQL translates them back to objects that we can work
within our own programming language.
- 33 -
Document Name
CONFIDENTIAL
LINQ to SQL
Object Relational Designer (O/R Designer) used to create
LINQ to SQL entity classes and associations (relationships) that
are based on objects in a database.
O/R Designer is used to create an object model in an application
that maps to objects in a database.
It also generates a strongly-typed DataContext that is used to
send and receive data between the entity classes and the
database.
O/R Designer also provides functionality to map stored
procedures and functions to DataContext methods for returning
data and populating entity classes.
- 34 -
Document Name
CONFIDENTIAL
LINQ to SQL
Object-relational mapping (ORM)
Records become strongly-typed objects
Includes tracking of changed objects and persistence
Ensures that you will not obtain multiple objects for the
same underlying row in the database
- 35 -
Document Name
CONFIDENTIAL
LINQ to SQL
Querying Data with LINQ to SQL
Select using LINQ to SQL
- 36 -
Document Name
CONFIDENTIAL
LINQ to SQL
Querying Data with LINQ to SQL
Insert with LINQ to SQL
// Northwnd inherits from System.Data.Linq.DataContext.
nw.SubmitChanges();
- 37 -
Document Name
CONFIDENTIAL
LINQ to SQL
Querying Data with LINQ to SQL
Update with LINQ to SQL
Document Name
CONFIDENTIAL
LINQ to SQL
Querying Data with LINQ to SQL
Delete with LINQ to SQL
- 39 -
Document Name
CONFIDENTIAL
LINQ to SQL
Querying Data with LINQ to SQL
Directly Execute SQL Commands
- 40 -
Document Name
CONFIDENTIAL
LINQ to SQL
Consisten
cy
Every object will be tracked by LINQ the moment it is
loaded from database.
Conflict checking when SubmitChanges() is called
- 41 -
Document Name
CONFIDENTIAL
ADO.NET
using(SqlConnection conn = new SqlConnection("Connection
String"))
{
conn.Open();
}
LINQ to SQL
NWDataContext db = new NWDataContext("Connection
String");
- 42 -
Document Name
CONFIDENTIAL
LINQ to SQL
using (NorthwindDataContext db = new NorthwindDataContext())
{ IEnumerable<Customer> custs =
from c in db.Customers
select c;
foreach (Customer c in custs) {
Console.WriteLine(c.CompanyName);
} }
- 43 -
Document Name
CONFIDENTIAL
LINQ to SQL
using (NorthwindDataContext db = new NorthwindDataContext()) {
Customer c = new Customer();
c.CustomerID = "ABCDE";
//.... add all the properties you need to add while inserting
db.Customers.InsertOnSubmit(c);
db.SubmitChanges();
}
- 44 -
Document Name
CONFIDENTIAL
LINQ to SQL
using (NorthwindDataContext db = new NorthwindDataContext()) {
Customer cust = (
from c in db.Customers
where c.CustomerID == "ALFKI"
select c).First();
- 45 -
Document Name
CONFIDENTIAL
LINQ to SQL
using (NorthwindDataContext db = new NorthwindDataContext()) {
Customer cust = (
from c in db.Customers
where c.CustomerID == "ALFKI"
select c).First();
db.Customers.DeleteOnSubmit(cust);
db.SubmitChanges();
}
- 46 -
Document Name
CONFIDENTIAL
LINQ to SQL
In LINQ to SQL it becomes method as you drag and drop it to .dbml file,
using (NorthwindDataContext db = new NorthwindDataContext())
{
var outPut = db.SalesByCategory("SeaFood", "1998");
}
- 47 -
Document Name
CONFIDENTIAL
LINQ to SQL
Transaction/Updat
e When update, first check whether new object is
added (by tracking mechanism) if yes, update
statement will be generated otherwise insert will occur
first.
Modification will not hit the database until the
SubmitChanges() method is called
All modifications will be encapsulated into a
transaction.
All operations will be translated into SQL statements
- 48 -
Document Name
CONFIDENTIAL
LINQ to SQL
Transaction/Updat
e
- 49 -
Document Name
CONFIDENTIAL
LINQ to Entities
LINQ to Entities
A more advanced ORM solution that allows more
extensive mapping and manipulation between how the
object appears and the underlying data source.
LINQ to Entity provides
Excellent build support; if it isn't mapped 100%, it
won't build.
It works with other databases as well.
It properly separates the structural model from the
conceptual entity model.
It maps many to many relationships properly.
- 51 -
Document Name
CONFIDENTIAL
LINQ to Entities
Querying Data with LINQ to Entities
Model your database using Entity Framework
ORM tool.
- Generates an EDMX file
GridView1.DataSource = authQuery;
GridView1.DataBind();
- 52 -
Document Name
CONFIDENTIAL
EF vs. LINQ-to-SQL
LINQ-to-SQL
Strongly typed LINQ access for RAD against SQL Server
only
Support s only direct Table-Per-Type (1-to-1) mapping
Limited out-of-the box support for complex scenarios
Entity Framework
- 54 -
Document Name
CONFIDENTIAL
EF vs. LINQ-to-SQL
Category
LINQ-to-SQL
Entity Framework
Model
domain model
conceptual data
model
Databases
Supported
SQL server,
OracleDB, DB2 and
many more
complex to use
Development Time
rapid development
slower development
but more
capabilities
Mapping
Direct 1-to-1
Custom mapping
Inheritance
hard to apply
simple to apply
- 55 -
Document Name
CONFIDENTIAL
Summary
Q&A
Want more.
Official site
http://msdn.microsoft.com/en-us/library/e80y5yhx.aspx
http://msdn.microsoft.com/en-us/library/vstudio/bb397926.a
spx
http://msdn.microsoft.com/en-us/library/bb399567.aspx
Tutorials
http://weblogs.asp.net/scottgu/archive/tags/LINQ/default.aspx
- 57 -
Document Name
CONFIDENTIAL
Thank You