expressions. (. ) .Select(c => new { c.Name, c.Phone });. Extension methods.
Object. Anonymous methods. Object initializers. Anonymous types. LINQ To
Objects.
Introduction I t d ti To T LINQ This material is based on the original slides of John Hidey, Mike Bird, Sharp Dudes, LLC
Data Access Data != Objects (Object Relational Mapping)
Build database-like search functionality into the y data: object, j , .NET Framework that works on any XML, SQL, custom types… LINQ: Language INtegrated Query
C# 3.0 Language Innovations
Local variable ttype inference i f
var contacts = Query from c in customers expressions where c.State == "WA" select new { c.Name, c.Phone }; Lambda expressions
var contacts t t = customers .Where(c ( => c.State == "WA")) .Select(c => new { c.Name, c.Phone }); Extension methods
Anonymous types
Object initializers
LINQ Q To Objects j Restriction
Where
Projection
Select, SelectMany
Ordering
OrderBy, ThenBy
Grouping
GroupBy
Joins
Join, GroupJoin
Quantifiers
Any, All
Partitioning
Take Skip, Take, Skip TakeWhile, TakeWhile SkipWhile
Sets
Distinct, Union, Intersect, Except
Elements
First, Last, Single, ElementAt
Aggregation
Count, Sum, Min, Max, Average
Conversion
ToArray, ToList, ToDictionary
Casting
OfType, Cast
LINQ Basics Query Operators can be used against any .NET collection (IEnumerable)
Built in examples: Select Built-in Select, Where Where, GroupBy GroupBy, Join, Join etc. etc Extensibility model supports adding/replacing them
Query Expressions can operate on information sources and apply query operators against them to return IEnumerable sequences
LINQ to SQL Language integrated data access
Maps tables and rows to classes and objects Builds on ADO ADO.NET NET and .NET NET Transactions
Mapping
Encoded in attributes or external XML file Relationships map to properties
Persistence
Automatic change tracking Updates through SQL or stored procedures
LINQ to SQL A Accessing i data d t today t d SqlConnection c = new SqlConnection(…); c.Open(); SqlCommand q cmd = new SqlCommand( q ( @"SELECT c.Name, c.Phone FROM Customers c WHERE c.City = @p0"); cmd Parameters AddWithValue("@p0" cmd.Parameters.AddWithValue( @p0 , "London“); London ); DataReader dr = c.ExecuteReader(cmd); while (dr.Read()) { string name = dr.GetString(0); string phone = dr.GetString(1); DateTime date = dr.GetDateTime(2); } dr Close(); dr.Close();
Queries in quotes
Loosely bound arguments
Loosely typed result sets
No compile time checks
LINQ to SQL Accessing A i data d t with ith LINQ public class Customer { … } public class Northwind : DataContext { public Table Customers; … } Northwind db = new Northwind(…); var contacts = from c in db.Customers where c.City == "London" select new { c.Name, c.Phone };
Classes describe data Tables are like collections Strongly typed connections I t Integrated t d query syntax Strongly St l typed t d results
LINQ to SQL Mapping Database
DataContext
Table
Class
View
Class
Column
Field / Property
Relationship
Field / Property
Stored Proced Procedure re
Method
LINQ to SQL Architecture from c in db.Customers where c.City == "London" select c.CompanyName
LINQ Query
Application
Objects
db.Customers.Add(c1); c2.City = “Seattle"; db.Customers.Remove(c3);
SubmitChanges()
LINQ to SQL SQL Query
Rows
SELECT CompanyName FROM Cust WHERE City = 'London'
DML or SProcs INSERT INTO Cust … UPDATE Cust … DELETE FROM Cust …
SQL Server
Key Points Flexible Fl ibl mapping i
“Classes first” or “data first”, attributes or mapping file
DataContext
Strongly typed database connection
Entity classes
Identity mapping and change tracking
Relationships
One-to-one, one-to-many
LINQ to SQL Access relational data as strongly typed objects Language L iintegrated t t d query Works with existing infrastructure Unified query and transform of objects, relational, XML
Resources The LINQ Project on MSDN (http://msdn2.microsoft.com/enus/netframework/aa904594.aspx) Charlie Calvert’s Blog (http://blogs.msdn.com/charlie/) Mike Taulty’s Blog (http://mtaulty.com/) LINQ in Action (http://linqinaction.net/) Scott Gu’s Blog (http://weblogs.asp.net/scottgu/default.aspx)