You are on page 1of 26

LINQ to SQL

LINQ to DataSet

LINQ to SQL allows querying a relational structure


by converting the LINQ query into a native SQL
query.
This means that the queries that you write in
code generates appropriate queries to access the
actual database, AUTOMATICALLY!
There are several concepts that you will come to
know in this module
How is the query written using object names (like tables
or views)?
How is it validated by the compiler?
When is the query generated?
When is the query executed?

You could either


Create a class and annotate it with the Table name
Define Fields (which correspond to columns)
Get the Data context (The DataContext class
handles the communication between LINQ and
external relational data sources)
Create a Generic type
Query through LINQ

OR use LINQ to SQL Classes in VS 2008

Data abstraction
Working with Strongly Typed objects
Tied to the object model and NOT to the
database
Query semantics is much more meaningful
Right now, ONLY SQL is supported, but going
forward there will be more providers which
would provide access to different databases
Once new providers come in, you could
connect to any database with virtually the
same code base

It plays a few important roles

Provide metadata to LINQ queries


Provide storage for data read from relational data source
Also helps in tracking updates
Supports submission back to the datasource

It does not need to be instantiated


It cant be a value type (struct)
Any class could be annotated with the Table attribute to declare an
entity
It can have any number and type of members
ONLY members annotated with Column is mapped to the table.
MUST have a key. If you dont have a key, you cannot update (although
you can still access the data)
IsPrimaryKey attribute needs to be changed to make the Column a
PrimaryKey
If a column name is Auto-generated by the database, you need to
provide IsDBGenerated parameter to true along with the DBType, like

Class entities can use Association attribute to


define relationships
Uses EntityRef to refer to the related table

So far, in the last few slides we have seen that


creating classes needs manually a lot of effort
It probably defeats the purpose of making
programming easy
Lets have a look at another way before you
make up your mind

Follow these steps with me

Create a new Windows Forms Application (C#)


Add a new item Linq to SQL Classes
Drag and drop Customer and Order tables
Switch to Form1
Go to Data Source window -> Add new data source
Select object (hit next)
Select LINQtoSQL -> Customer (click next followed with
Finish)
Drag and drop customers (dock the control)
Go to Form_load and write the following code

NorthwindDataContext db = new NorthwindDataContext();

Now, you are all set just start querying!!!

Write the following in Form1_Load and hit


F5!!

By default, you will be able to add the


records, and edit them as well, but the SAVE
button is enabled.
Right click on the Save button and select
properties
Make enabled property = true and double
click on the save button

Add a textbox and a button to the toolstrip


and in the buttons click event put in the
following code

Show how you can attach to an SP


Show method options
Check the DelayLoaded property in the
designer
Modify the custom logic for Delete, Update,
Insert

Drag and drop customer and orders


Add a new Windows Form
Click on Data Menu -> Show data sources
Add a new data source and select object and click
next
Select Customer from the tree and click next
Drag and drop Customer
Now, drag and drop Orders
Create the datacontext in the main class called
db.
Assign the customerBindingSources DataSource
to Customers
Add db.log = console.out in form_load

LINQ to DataSet interacts with data already in


ADO.NET DataSet structures.
A dataset could be considered as an in memory
database, and hence it is a good target for a LINQ
implementation
A DataTable can be queried with LINQ, just as any
other IEnumerable<T> list.
NOTE > DataTable does not implement
IEnumerable<T>. You have to call AsEnumerable,
which is an extension method for DataTable, to
obtain a wrapper that implements that interface.
A typed DataSet can be queried with a simpler syntax
because it is not necessary to use the Field<T>
accessor and the AsEnumerable method.

You might also like