USING LINQ TO SQL

Transcription

USING LINQTO SQLByScott GuthrieGreat ‘LINQ To Sql’ Tutorial Series FromScottGu’s BlogCompilation By Kadir Pekelhttp://it-box.blogturk.net

Part 1 - IntroductionOver the last few months I wrote a series of blog posts that covered some of the new languagefeatures that are coming with the Visual Studio and .NET Framework "Orcas" release. Hereare pointers to the posts in my series: Automatic Properties, Object Initializer and Collection InitializersExtension MethodsLambda ExpressionsQuery SyntaxAnonymous TypesThe above language features help make querying data a first class programming concept. Wecall this overall querying programming model "LINQ" - which stands for .NET LanguageIntegrated Query.Developers can use LINQ with any data source. They can express efficient query behavior intheir programming language of choice, optionally transform/shape data query results intowhatever format they want, and then easily manipulate the results. LINQ-enabled languagescan provide full type-safety and compile-time checking of query expressions, anddevelopment tools can provide full intellisense, debugging, and rich refactoring support whenwriting LINQ code.LINQ supports a very rich extensibility model that facilitates the creation of very efficientdomain-specific operators for data sources. The "Orcas" version of the .NET Frameworkships with built-in libraries that enable LINQ support against Objects, XML, and Databases.What Is LINQ to SQL?LINQ to SQL is an O/RM (object relational mapping) implementation that ships in the .NETFramework "Orcas" release, and which allows you to model a relational database using .NETclasses. You can then query the database using LINQ, as well as update/insert/delete datafrom it.LINQ to SQL fully supports transactions, views, and stored procedures. It also provides aneasy way to integrate data validation and business logic rules into your data model.Modeling Databases Using LINQ to SQL:Visual Studio "Orcas" ships with a LINQ to SQL designer that provides an easy way to modeland visualize a database as a LINQ to SQL object model. My next blog post will cover inmore depth how to use this designer (you can also watch this video I made in January to seeme build a LINQ to SQL model from scratch using it).Using the LINQ to SQL designer I can easily create a representation of the sample"Northwind" database like below:

My LINQ to SQL design-surface above defines four entity classes: Product, Category, Orderand OrderDetail. The properties of each class map to the columns of a corresponding table inthe database. Each instance of a class entity represents a row within the database table.The arrows between the four entity classes above represent associations/relationships betweenthe different entities. These are typically modeled using primary-key/foreign-keyrelationships in the database. The direction of the arrows on the design-surface indicatewhether the association is a one-to-one or one-to-many relationship. Strongly-typedproperties will be added to the entity classes based on this. For example, the Category classabove has a one-to-many relationship with the Product class. This means it will have a"Categories" property which is a collection of Product objects within that category. TheProduct class then has a "Category" property that points to a Category class instance thatrepresents the Category to which the Product belongs.The right-hand method pane within the LINQ to SQL design surface above contains a list ofstored procedures that interact with our database model. In the sample above I added a single"GetProductsByCategory" SPROC. It takes a categoryID as an input argument, and returns asequence of Product entities as a result. We'll look at how to call this SPROC in a codesample below.Understanding the DataContext ClassWhen you press the "save" button within the LINQ to SQL designer surface, Visual Studiowill persist out .NET classes that represent the entities and database relationships that we

modeled. For each LINQ to SQL designer file added to our solution, a custom DataContextclass will also be generated. This DataContext class is the main conduit by which we'll queryentities from the database as well as apply changes. The DataContext class created will haveproperties that represent each Table we modeled within the database, as well as methods foreach Stored Procedure we added.For example, below is the NorthwindDataContext class that is persisted based on the modelwe designed above:LINQ to SQL Code ExamplesOnce we've modeled our database using the LINQ to SQL designer, we can then easily writecode to work against it. Below are a few code examples that show off common data tasks:1) Query Products From the DatabaseThe code below uses LINQ query syntax to retrieve an IEnumerable sequence of Productobjects. Note how the code is querying across the Product/Category relationship to onlyretrieve those products in the "Beverages" category:C#:

VB:2) Update a Product in the DatabaseThe code below demonstrates how to retrieve a single product from the database, update itsprice, and then save the changes back to the database:C#:VB:Note: VB in "Orcas" Beta1 doesn't support Lambdas yet. It will, though, in Beta2 - at whichpoint the above query can be rewritten to be more concise.3) Insert a New Category and Two New Products into the DatabaseThe code below demonstrates how to create a new category, and then create two new productsand associate them with the category. All three are then saved into the database.

Note below how I don't need to manually manage the primary key/foreign keyrelationships. Instead, just by adding the Product objects into the category's "Products"collection, and then by adding the Category object into the DataContext's "Categories"collection, LINQ to SQL will know to automatically persist the appropriate PK/FKrelationships for me.C#VB:4) Delete Products from the Database

The code below demonstrates how to delete all Toy products from the database:C#:VB:5) Call a Stored ProcedureThe code below demonstrates how to retrieve Product entities not using LINQ query syntax,but rather by calling the "GetProductsByCategory" stored procedure we added to our datamodel above. Note that once I retrieve the Product results, I can update/delete them and thencall db.SubmitChanges() to persist the modifications back to the database.C#:

VB:6) Retrieve Products with Server Side PagingThe code below demonstrates how to implement efficient server-side database paging as partof a LINQ query. By using the Skip() and Take() operators below, we'll only return 10 rowsfrom the database - starting with row 200.C#:VB:SummaryLINQ to SQL provides a nice, clean way to model the data layer of your application. Onceyou've defined your data model you can easily and efficiently perform queries, inserts,updates and deletes against it.

Hopefully the above introduction and code samples have helped whet your appetite to learnmore. Over the next few weeks I'll be continuing this series to explore LINQ to SQL in moredetail

Part 2 - Defining our Data Model ClassesIn Part 1 of my LINQ to SQL blog post series I discussed "What is LINQ to SQL" andprovided a basic overview of some of the data scenarios it enables.In my first post I provided code samples that demonstrated how to perform common datascenarios using LINQ to SQL including: How to query a databaseHow to update rows in a databaseHow to insert and relate multiple rows in a databaseHow to delete rows in a databaseHow to call a stored procedureHow to retrieve data with server-side pagingI performed all of these data scenarios using a LINQ to SQL class model that looked like theone below:In this second blog post in the series I'm going to go into more detail on how to create theabove LINQ to SQL data model.LINQ to SQL, the LINQ to SQL Designer, and all of the features that I'm covering in thisblog post series will ship as part of the .NET 3.5 and Visual Studio "Orcas" release.

You can follow all of the steps below by downloading either Visual Studio "Orcas" Beta 1 orVisual Web Developer Express "Orcas" Beta1. Both can be installed and used side-by-sidewith VS 2005.Create a New LINQ to SQL Data ModelYou can add a LINQ to SQL data model to an ASP.NET, Class Library or Windows clientproject by using the "Add New Item" option within Visual Studio and selecting the "LINQ toSQL" item within it:Selecting the "LINQ to SQL" item will launch the LINQ to SQL designer, and allow you tomodel classes that represent a relational database. It will also create a stronglytyped "DataContext" class that will have properties that represent each Table we modeledwithin the database, as well as methods for each Stored Procedure we modeled. As Idescribed in Part 1 of this blog post series, the DataContext class is the main conduit by whichwe'll query entities from the database as well as apply changes back to it.Below is a screen-shot of an empty LINQ to SQL ORM designer surface, and is what you'llsee immediately after creating a new LINQ to SQL data model:

Entity ClassesLINQ to SQL enables you to model classes that map to/from a database. These classes aretypically referred to as "Entity Classes" and instances of them are called "Entities". Entityclasses map to tables within a database. The properties of entity classes typically map to thetable's columns. Each instance of an entity class then represents a row within the databasetable.Entity classes defined with LINQ to SQL do not have to derive from a specific base class,which means that you can have them inherit from any object you want. All classes createdusing the LINQ to SQL designer are defined as "partial classes" - which means that you canoptionally drop into code and add additional properties, methods and events to them.Unlike the DataSet/TableAdapter feature provided in VS 2005, when using the LINQ to SQLdesigner you do not have to specify the SQL queries to use when creating your data modeland access layer.Instead, you focus on defining your entity classes, how they map to/from the database, and therelationships between them. The LINQ to SQL OR/M implementation will then take care ofgenerating the appropriate SQL execution logic for you at runtime when you interact and usethe data entities. You can use LINQ query syntax to expressively indicate how to query yourdata model in a strongly typed way.Creating Entity Classes From a DatabaseIf you already have a database schema defined, you can use it to quickly create LINQ to SQLentity classes modeled off of it.The easiest way to accomplish this is to open up a database in the Server Explorer withinVisual Studio, select the Tables and Views you want to model in it, and drag/drop them ontothe LINQ to SQL designer surface:

When you add the above 2 tables (Categories and Products) and 1 view (Invoices) from the"Northwind" database onto the LINQ to SQL designer surface, you'll automatically have thefollowing three entity classes created for you based on the database schema:

Using the data model classes defined above, I can now run all of the code samples (expect theSPROC one) described in Part 1 of this LINQ to SQL series. I don't need to add anyadditional code or configuration in order to enable these query, insert, update, delete, andserver-side paging scenarios.Naming and PluralizationOne of the things you'll notice when using the LINQ to SQL designer is that it automatically"pluralizes" the various table and column names when it creates entity classes based on yourdatabase schema. For example: the "Products" table in our example above resulted in a"Product" class, and the "Categories" table resulted in a "Category" class. This class naminghelps make your models consistent with the .NET naming conventions, and I usually findhaving the designer fix these up for me really convenient (especially when adding lots oftables to your model).If you don't like the name of a class or property that the designer generates, though, you canalways override it and change it to any name you want. You can do this either by editing theentity/property name in-line within the designer or by modifying it via the property grid:

The ability to have entity/property/association names be different from your database schemaends up being very useful in a number of cases. In particular:1) When your backend database table/column schema names change. Because your entitymodels can have different names from the backend schema, you can decide to just updateyour mapping rules and not update your application or query code to use the newtable/column name.2) When you have database schema names that aren't very "clean". For example, rather thanuse "au lname" and "au fname" for the property names on an entity class, you can just namethem to "LastName" and "FirstName" on your entity class and develop against that instead(without having to rename the column names in the database).Relationship AssociationsWhen you drag objects from the server explorer onto the LINQ to SQL designer, VisualStudio will inspect the primary key/foreign key relationships of the objects, and based onthem automatically create default "relationship associations" between the different entityclasses it creates. For example, when I added both the Products and Categories tables fromNorthwind onto my LINQ to SQL designer you can see that a one to many relationshipbetween the two is inferred (this is denoted by the arrow in the designer):The above association will cause cause the Product entity class to have a "Category" propertythat developers can use to access the Category entity for a given Product. It will also causethe Category class to have a "Products" collection that enables developers to retrieve allproducts within that Category.

If you don't like how the designer has modeled or named an association, you can alwaysoverride it. Just click on the association arrow within the designer and access its propertiesvia the property grid to rename, delete or modify it.Delay/Lazy LoadingLINQ to SQL enables developers to specify whether the properties on entities should beprefetched or delay/lazy-loaded on first access. You can customize the default prefetch/delay-load rules for entity properties by selecting any entity property or association inthe designer, and then within the property-grid set the "Delay Loaded" property to true orfalse.For a simple example of when I'd want to-do this, consider the "Category" entity class wemodeled above. The categories table inside "Northwind" has a "Picture" column which storesa (potentially large) binary image of each category, and I only want to retrieve the binaryimage from the database when I'm actually using it (and not when doing a simply query justto list the category names in a list).I could configure the Picture property to be delay loaded by selecting it within the LINQ toSQL designer and by settings its Delay Loaded value in the property grid:

Note: In addition to configuring the default pre-fetch/delay load semantics on entities, you canalso override them via code when you perform LINQ queries on the entity class (I'll showhow to-do this in the next blog post in this series).Using Stored ProceduresLINQ to SQL allows you to optionally model stored procedures as methods on yourDataContext class. For example, assume we've defined the simple SPROC below to retrieveproduct information based on a categoryID:I can use the server explorer within Visual Studio to drag/drop the SPROC onto the LINQ toSQL designer surface in order to add a strongly-typed method that will invoke the SPROC. IfI drop the SPROC on top of the "Product" entity in the designer, the LINQ to SQL designerwill declare the SPROC to return an IEnumerable Product result:

I can then use either LINQ Query Syntax (which will generate an adhoc SQL query) oralternatively invoke the SPROC method added above to retrieve product entities from thedatabase:Using SPROCs to Update/Delete/Insert Data

By default LINQ to SQL will automatically create the appropriate SQL expressions for youwhen you insert/update/delete entities. For example, if you wrote the LINQ to SQL codebelow to update some values on a "Product" entity instance:By default LINQ to SQL would create and execute the appropriate "UPDATE" statement foryou when you submitted the changes (I'll cover this more in a later blog post on updates).You can also optionally define and use custom INSERT, UPDATE, DELETE sprocs instead.To configure these, just click on an entity class in the LINQ to SQL designer and within itsproperty-grid click the "." button on the Delete/Insert/Update values, and pick a particularSPROC you've defined instead:What is nice about changing the above setting is that it is done purely at the mapping layer ofLINQ to SQL - which means the update code I showed earlier continues to work with nomodifications required. This avoids developers using a LINQ to SQL data model from havingto change code even if they later decide to put in a custom SPROC optimization later.Summary

LINQ to SQL provides a nice, clean way to model the data layer of your application. Onceyou've defined your data model you can easily and efficiently perform queries, inserts,updates and deletes against it.Using the built-in LINQ to SQL designer within Visual Studio and Visual Web DeveloperExpress you can create and manage your data models for LINQ to SQL extremely fast. TheLINQ to SQL designer also provides a lot of flexibility that enables you to customize thedefault behavior and override/extend the system to meet your specific needs.In upcoming posts I'll be using the data model we created above to drill into querying, inserts,updates and deletes further. In the update, insert and delete posts I'll also discuss how to addcustom business/data validation logic to the entities we designed above to perform additionalvalidation logic.Mike Taulty also has a number of great LINQ to SQL videos that I recommend checking outhere. These provide a great way to learn by watching someone walkthrough using LINQ toSQL in action.

Part 3 - Querying our DatabaseLast month I started a blog post series covering LINQ to SQL. LINQ to SQL is a built-inO/RM (object relational mapping) framework that ships in the .NET Framework 3.5 release,and which enables you to easily model relational databases using .NET classes. You can thenuse LINQ expressions to query the database with them, as well as update/insert/delete datafrom it.Below are the first two parts of my LINQ to SQL series: Part 1: Introduction to LINQ to SQLPart 2: Defining our Data Model ClassesIn today's blog post I'll be going into more detail on how to use the data model we created inthe Part 2 post, and show how to use it to query data within an ASP.NET project.Northwind Database Modeled using LINQ to SQLIn Part 2 of this series I walked through how to create a LINQ to SQL class model using theLINQ to SQL designer that is built-into VS 2008. Below is the class model that we createdfor the Northwind sample database:Retrieving Products

Once we have defined our data model classes above, we can easily query and retrieve datafrom our database. LINQ to SQL enables you to do this by writing LINQ syntax queriesagainst the NorthwindDataContext class that we created using the LINQ to SQL designerabove.For example, to retrieve and iterate over a sequence of Product objects I could write code likebelow:In the query above I have used a "where" clause in my LINQ syntax query to only returnthose products within a specific category. I am using the CategoryID of the Product toperform the filter.One of the nice things above LINQ to SQL is that I have a lot of flexibility in how I query mydata, and I can take advantage of the associations I've setup when modeling my LINQ to SQLdata classes to perform richer and more natural queries against the database. For example, Icould modify the query to filter by the product's CategoryName instead of its CategoryID bywriting my LINQ query like so:Notice above how I'm using the "Category" property that is on each of the Product objects tofilter by the CategoryName of the Category that the Product belongs to. This property wasautomatically created for us by LINQ to SQL because we modeled the Category and Productclasses as having a many to one relationship with each other in the database.

For another simple example of using our data model's association relationships within queries,we could write the below LINQ query to retrieve only those products that have had 5 or moreorders placed for them:Notice above how we are using the "OrderDetails" collection that LINQ to SQL has createdfor us on each Product class (because of the 1 to many relationship we modeled in the LINQto SQL designer).Visualizing LINQ to SQL Queries in the DebuggerObject relational mappers like LINQ to SQL handle automatically creating and executing theappropriate SQL code for you when you perform a query or update against their objectmodel.One of the biggest concerns/fears that developers new to ORMs have is "but what SQL codeis it actually executing?" One of the really nice things about LINQ to SQL is that it makes itsuper easy to see exactly what SQL code it is executing when you run your application withinthe debugger.Starting with Beta2 of Visual Studio 2008 you can use a new LINQ to SQL visualizer plug-into easily see (and test out) any LINQ to SQL query expression. Simply set a breakpoint andthen hover over a LINQ to SQL query and click the magnify glass to pull up its expressionvisualizer within the debugger:This will then bring up a dialog that shows you the exact SQL that LINQ to SQL will usewhen executing the query to retrieve the Product objects:

If you press the "Execute" button within this dialog it will allow you to evaluate the SQLdirectly within the debugger and see the exact data results returned from the database:This obviously makes it super easy to see precisely what SQL query logic LINQ to SQL isdoing for you. Note that you can optionally override the raw SQL that LINQ to SQL executesin cases where you want to change it - although in 98% of scenarios I think you'll find that theSQL code that LINQ to SQL executes is really, really good.Databinding LINQ to SQL Queries to ASP.NET ControlsLINQ queries return results that implement the IEnumerable interface - which is also aninterface that ASP.NET server controls support to databind object. What this means is that

you can databind the results of any LINQ, LINQ to SQL, or LINQ to XML query to anyASP.NET control.For example, we could declare an asp:gridview control in a .aspx page like so:I could then databind the result of the LINQ to SQL query we wrote before to the GridViewlike so:This will then generate a page that looks like below:

Shaping our Query ResultsRight now when we are evaluating our product query, we are retrieving by default all of thecolumn data needed to populate the Product entity classes.For example, this query to retrieve products:Results in all of this data being returned:Often we only want to return a subset of the data about each product. We can use the newdata shaping features that LINQ and the new C# and VB compilers support to indicate that weonly want a subset of the data by modifying our LINQ to SQL query like so:

This will result in only this data subset being returned from our database (as seen via ourdebug visualizer):What is cool about LINQ to SQL is that I can take full advantage of my data model classassociations when shaping my data. This enables me to express really useful (and veryefficient) data queries. For example, the below query retrieves the ID and Name from theProduct entity, the total number of orders that have been made for the Product, and then sumsup the total revenue value of each of the Product's orders:The expression to the right of the "Revenue" property above is an example of using the "Sum"extension method provided by LINQ. It takes a Lambda expression that returns the value ofeach product order item as an argument.LINQ to SQL is smart and is able to convert the above LINQ expression to the below SQLwhen it is evaluated (as seen via our debug visualizer):

The above SQL causes all of the NumOrders and Revenue value computations to be doneinside the SQL server, and results in only the below data being retrieved from the database(making it really fast):We can then databind the result sequence against our GridView control to generate pretty UI:

BTW - in case you were wondering, you do get full intellisense within VS 2008 when writingthese types of LINQ shaping queries:In the example above I'm declaring an anonymous type that uses object initialization to shapeand define the result structure. What is really cool is that VS 2008 provides full intellisense,compilation checking, and refactoring support when working against these anonymous resultsequences as well:

Paging our Query ResultsOne of the common needs in web scenarios is to be able to efficiently build data paging UI.LINQ provides built-in support for two extension methods that make this both easyand efficient - the Skip() and Take() methods.We can use the Skip() and Take() methods below to indicate that we only want to return 10product objects - starting at an initial product row that we specify as a parameter argument:Note above how I did not add the Skip() and Take() operator on the initial products querydeclaration - but instead added it later to the query (when binding it to my GridViewdatasource). People often ask me "but doesn't this mean that the query first grabs all the datafrom the database and then does the paging in the middle tier (which is bad)?" No. Thereason is that LINQ uses a deferred execution model - which means that the query doesn'tactually execute until you try and iterate over the results.

One of the benefits of this deferred execution model is that it enables you to nicely composequeries across multiple code statements (which improves code readability). It also enablesyou to compose queries out of other queries - which enables some very flexible querycomposition and re-use scenarios.Once I have the BindProduct() method defined above, I can write the code below in my pageto retrieve the starting index from the querystring and cause the products to be paged anddisplayed in the gridview:This will then give us a products page, filtered to list only those products with more than 5orders, showing dynamically computed product data, and which is pageable via a querystringargument:Note: When working against SQL 2005, LINQ to SQL will use the ROW NUMBER() SQLfunction to perform all of the data paging logic in the database. This ensures that only the 10rows of data we want in the current page view are returned from the database when weexecute the above code:

This makes it efficient and easy to page over large data sequences.SummaryHopefully the above walkthrough provides a good overview of some of the cool data queryopportunities that LINQ to SQL provides. To learn more about LINQ expressions and thenew language syntax supported by the C# and VB compilers with VS 2008, please read theseearlier posts of mine: Automatic Properties, Object Initializer and Collection InitializersExtension MethodsLambda ExpressionsQuery SyntaxAnonymous TypesIn my next post in this LINQ to SQL series I'll cover how we can cleanly add validation logicto our data model classes, and demonstrate how we can use it to encapsulate business logicthat executes every time we update, insert, or delete our data. I'll then cover more advancedlazy and eager loading query scenarios, how to use the new asp:LINQDataSource controlto support declarative databinding of ASP.NET controls, optimistic concurrency errorresolution, and more.

Part 4 - Updating our DatabaseOver the last few weeks I've been writing a series of blog posts that cover LINQ to SQL.LINQ to SQL is a built-in O/RM (object relational mapper) that ships in the .NET Framework3.5 release, and which enables you to easily model relational databases using .NET classes.You can use LINQ expressions to query the database with them, as well asupdate/insert/delete data.Below are the first three parts of my LINQ to SQL series: Part 1: Introduction to LINQ to SQLPart 2: Defining our Data Model ClassesPart 3: Querying our DatabaseIn today's blog post I'll cover how we we can use the data model we created earlier, and use itto update, insert, and delete data. I'll also show how we can cleanly integrate business rulesand custom validation logic with our data model.Northwind Database Modeled using LINQ to SQLIn Part 2 of this series I walked through how to create a LINQ to SQL class model using theLINQ to SQL designer that is built-into VS 2008. Below is a class model created for theNorthwind sample database and which I'll be using in this blog post:

When we designed our data model using the LINQ to SQL data designer above we definedfive data model classes: Product, Category, Customer, Order and OrderDetail. The propertiesof each class map to the columns of a corresponding table in the database. Each instance of aclass entity represents a row within the database table.When we defined our data model, the LINQ to SQL designer also created a customDataContext class that provides the main conduit by which we'll query our database and applyupdates/changes. In the example data model we defined above this class was named"NorthwindDataContext". The NorthwindDataContext class has properties that representeach Table we modeled within the database (specifically: Products, Categories,Customers, Orders, OrderDetails).As I covered in Part 3 of this blog series, we can easily use LINQ syntax expressions to queryand retrieve data from our database using this NorthwindDataContext class. LINQ to SQLwill then automatically translate these LINQ query expressions to th

The code below demonstrates how to implement efficient server-side database paging as part of a LINQ query. By using the Skip() and Take() operators below, we'll only return 10 rows from the database - starting with row 200. C#: VB: Summary LINQ to SQL provides a nice, cl