1devInTown 1
devInTown
Chan Ming ManC# MVPhttp://chanmingman.spaces.live.comLevel 200
2devInTown 2
devInTown
Chan Ming ManC# MVPhttp://chanmingman.spaces.live.comLevel 200
CRUD Using LINQ (Language Integrated Query)
3devInTowndevInTown
Agenda
The object model challenges the relational model
What is LINQ?
LINQ to SQL
4devInTown
Helpful Experience
Visual Studio 2008 C# Express
SQL Server 2005
ADO.NET
5devInTowndevInTown
The object model challenges the relational model
The relational model has been fantastically successful in a wide variety of application areas. However, it is not problem free. The problems have been made more visible by the rise in popularity of object-oriented programming languages such as C++, Java, and C#.
6devInTowndevInTown
Data…its our job
Querying and manipulating data has always been a fundamental part of our jobs as programmers
Data formats change, but core needs are the same
7devInTowndevInTown
Data Access(DBASE circa 1980s) USE empl
REPLACE ALL salary WITH (salary * 1.1) FOR supervises > 0
LIST ALL fname, lname, salary FOR Supervises > 0
Data querying and manipulation a core part of the programming model experience
Certainly had limitations, but it sure was useful
8devInTowndevInTown
But Challenges Still Remain…How to retrieve non-relational data?
XML, RSS, Web Services, REST, AD, Files, etc.
How to interact with plain old objects?How do you interact and query custom domain models?
How to enable rich data shaping & transformations?Support flexible query composition (that is fast!)
How to enable clean code in both a strongly typed and dynamic language world?
9devInTown
LINQ
Query, Set, and Transform Operations for .NET
Makes querying data a core programming concept
Works with all types and shapes of dataRelational databasesXMLPlain old Objects
Works with all .NET languagesNew VB and C# have integrated language support
10devInTown
What is LINQ?
Strongly-typedBenefits from IntelliSense
Language INtegrated Queries
Collections of objectsRelational dataXML
Queries integrated with code (C#/VB)
Unified Querying
11devInTown
LINQ Enabled Data Sources
LINQ enabled ADO.NET
LINQ Architecture
LINQ to
Entities
LINQ To
SQL
LINQ to
XML
C# 3.0 VB 9.0 Others…
LINQ To
Dataset
LINQ To
Objects
XMLObjects Relational Data
.NET Language-Integrated Query (LINQ)
11©2008 Pavel Yosifovich
12devInTown
Where is LINQ to SQL in MVC?
Model
ControllerView
LINQ To
SQL
13devInTowndevInTown
LINQ to SQL
Application
SQL Server
LINQ to SQL
from c in db.Customerswhere c.City == "London"select c.CompanyName
LINQ Query
SQL Query
SELECT CompanyNameFROM CustWHERE City = 'London'
Rows
Objects SubmitChanges()
DML or Stored Procedures
db.Customers.Add(c1);c2.City = “Seattle";db.Customers.Remove(c3);
INSERT INTO Customers…UPDATE Customers …DELETE FROM Customers …
14devInTowndevInTown
LINQ to SQL fully support CRUD
C = Create = InsertR = Read = SelectU = UpdateD = Delete
15devInTowndevInTown
CreateLINQ
var newCustomer = new Customer {CustomerID = "DLEAP",CompanyName = "DevLeap",Country = "Italy" };db.Customers.InsertOnSubmit(newCustomer);
INSERT INTO [Customers](CustomerID, CompanyName, ...)VALUES("DLEAP", "DevLeap", ...)
SQL
16devInTowndevInTown
Read
var query =from c in Customerswhere c.Country == "USA"&& c.State == "WA"select new {c.CustomerID, c.CompanyName, c.City };
SELECT CustomerID, CompanyName, CityFROM CustomersWHERE Country = 'USA'AND Region = 'WA'
LINQ
SQL
17devInTowndevInTown
Update
Customer cust = db.Customers.Single(p => p.CustomerID == 11111);cust.LastName = “Thomas ";db.SubmitChanges();
UPDATE Customer LastName = “Thomas "SET WHERE CustomerID == 11111
SQL
LINQ
18devInTowndevInTown
Delete
var oldDetail = db.Order_Details.Single(od => od.OrderID == 10422&& od.ProductID == 26);db.Order_Details.DeleteOnSubmit(oldDetail);
DELETE FROM [dbo].[Order Details]WHERE [OrderID] = 10422 AND [ProductID] = 26 AND ...
SQL
LINQ
19devInTown 19
Demo …
20devInTown
Resources - LINQ
MSDN Developer Centerhttp://msdn2.microsoft.com/en-us/netframework/aa904594.aspx
LINQ Forumhttp://forums.microsoft.com/msdn/showforum.aspx?forumid=123
Channel9 Linq Videoshttp://channel9.msdn.com/tags/linq
BlogsScott Guthrie
http://weblogs.asp.net/scottgu/archive/2007/04/21/new-orcas-language-feature-query-syntax.aspx
Charlie Calvert http://blogs.msdn.com/charlie/archive/2006/10/05/Links-to-LINQ.aspx
LINQ HOLC# 3.0 Enhancements & LINQ (English)
21devInTown
© 2008 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries.
The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after
the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.