Introduction to LINQ - Ganesan Muthiah's Blog

Download Report

Transcript Introduction to LINQ - Ganesan Muthiah's Blog

Introduction to LINQ
Ganesan Muthiah
http://gmuthaia.wordpress.com/
[email protected]
Introduction

How do you interrogate/manipulate data?

What if you could do the work in a typesafe," string-free manner?

What if you could use a consistent
querying syntax for data, objects or XML?
Agenda

What is LINQ

Queries without LINQ

Key features of LINQ

LINQ Architecture

LINQ to…
What is LINQ?

Language Integrated Query

Make query a part of the language

Component of .NET Framework 3.5
Queries without LINQ

Objects using loops and conditions
foreach(Customer c in customers)
if (c.Region == "USA") ...

SELECT from database tables
SELECT * FROM Customers WHERE
Region='USA'
XML using XPath/XQuery
//Customers/Customer[@Region='USA']

Key Features of LINQ

Delayed Execution
 LINQ queries don't execute until they must
 Retrieve specific values
 Iterate through the collection
 Perform an operation

Write Data Access Code directly

Compile time syntax and schema checking (intellisense too)
• no need of inline sql or to wait until runtime to see if it is ok

LINQ data access code abstracted from underlying data
• consistent syntax across various data sources
• can join information from different sources.
Architecture
LINQ to…

LINQ to Objects

LINQ to SQL (formerly known as
DLINQ)

LINQ to XML (formerly known as
XLINQ)

LINQ to Entities (ADO.NET Entities)
LINQ to Objects

Query any IEnumerable<T> source
Includes arrays, List<T>, Dictionary...

Many useful operators available
Sum, Max, Min, Distinct, Intersect, Union

Expose your own data with
IEnumerable<T> or IQueryable<T>

Create operators using extension methods
LINQ to Objects
DEMO
LINQ to SQL (formerly known as DLinq)




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 ( Fowler’s Identity Map)
Data context is the controller mechanism
Facilitates update, delete & insert
Type, parameter and injection safe
LINQ to SQL
DEMO
LINQ to XML (formerly known as XLinq)

Similar to LINQ to SQL but along the idea
of querying XML documents using LINQ
syntax rather than the XPath/XQuery
syntax.
LINQ to XML
DEMO
LINQ to Entities

LINQ to Entities (ADO.NET 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 (I'm currently using it with Oracle)
It properly seperates the structural model from the conceptual entity
model.
It maps many to many relationships properly.
LINQ to Entities
DEMO
Summary

Had a quick intro on
LINQ and its need
Key Language features
LINQ Architecture
LINQ Types
LINQ Operators
For more information….

Official site
http://msdn.microsoft.com/en-us/netframework/aa904594.aspx

Tutorials
http://weblogs.asp.net/scottgu/archive/tags/LINQ/default.aspx

101 LINQ Samples
http://msdn.microsoft.com/en-us/vcsharp/aa336746.aspx

LINQ Pad
http://www.linqpad.net/
Thank you
Questions?