﻿<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/">
  <channel>
    <dc:language>en-US</dc:language>
    <generator>FeedGhost</generator>
    <title>Great .Net Blog Posts</title>
    <link>http://www.feedghost.com/UserArea/LinkBlog/Htm.aspx?Id=33</link>
    <description>A collection of some of the best .Net related blog posts i've come across. well worth a read!</description>
    <item>
      <title>LINQ to SQL (Part 2 - Defining our Data Model Classes)</title>
      <pubDate>Sat, 30 Jun 2007 16:11:17 +0100</pubDate>
      <guid isPermaLink="false">876286f5-df96-887e-2ab6-b3c52bb685ae</guid>
      <description>&lt;a href="http://weblogs.asp.net/scottgu/archive/2007/05/29/linq-to-sql-part-2-defining-our-data-model-classes.aspx"&gt;Linked article&lt;/a&gt; originally from &lt;a href="http://weblogs.asp.net/scottgu/default.aspx"&gt;ScottGu's Blog&lt;/a&gt;&lt;br /&gt;&lt;div&gt;&lt;/div&gt;&lt;br /&gt;&lt;font face="arial" size="2"&gt; &lt;p&gt;In &lt;a href="http://weblogs.asp.net/scottgu/archive/2007/05/19/using-linq-to-sql-part-1.aspx" target="_blank"&gt;Part 1&lt;/a&gt; of my LINQ to SQL blog post series I discussed "What is LINQ to SQL" and provided a basic overview of some of the data scenarios it enables.&lt;/p&gt; &lt;p&gt;In my first post I provided code samples that demonstrated how to perform common data scenarios using LINQ to SQL including:&lt;/p&gt; &lt;ul&gt; &lt;li&gt;How to query a database  &lt;li&gt;How to update rows in a database  &lt;li&gt;How to insert and relate multiple rows in a database  &lt;li&gt;How to delete rows in a database  &lt;li&gt;How to call a stored procedure  &lt;li&gt;How to retrieve data with server-side paging&lt;/li&gt;&lt;/li&gt;&lt;/li&gt;&lt;/li&gt;&lt;/li&gt;&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;I performed all of these data scenarios using a LINQ to SQL class model that looked like the one below:&lt;/p&gt;&lt;img src="http://www.scottgu.com/blogposts/linqtosql/step1.jpg" /&gt;  &lt;p&gt;In this second blog post in the series I'm going to go into more detail on how to create the above LINQ to SQL data model.&lt;/p&gt; &lt;p&gt;LINQ to SQL, the LINQ to SQL Designer, and all of the features that I'm covering in this blog post series will ship as part of the .NET 3.5 and Visual Studio "Orcas" release.  &lt;/p&gt; &lt;p&gt;You can follow all of the steps below by downloading either &lt;a href="http://www.microsoft.com/downloads/details.aspx?FamilyID=5d9c6b2d-439c-4ec2-8e24-b7d9ff6a2ab2&amp;amp;DisplayLang=en" target="_blank"&gt;Visual Studio "Orcas" Beta 1&lt;/a&gt; or &lt;a href="http://msdn.microsoft.com/vstudio/express/future/downloads/default.aspx" target="_blank"&gt;Visual Web Developer Express "Orcas" Beta1&lt;/a&gt;.  Both can be installed and used side-by-side with VS 2005.&lt;/p&gt; &lt;h3&gt;&lt;u&gt;Create a New LINQ to SQL Data Model&lt;/u&gt;&lt;/h3&gt; &lt;p&gt;You can add a LINQ to SQL data model to an ASP.NET, Class Library or Windows client project by using the "Add New Item" option within Visual Studio and selecting the "LINQ to SQL" item within it:&lt;/p&gt; &lt;p&gt;&lt;img src="http://www.scottgu.com/blogposts/linqtosql2/step1.jpg" /&gt; &lt;/p&gt; &lt;p&gt;Selecting the "LINQ to SQL" item will launch the LINQ to SQL designer, and allow you to model classes that represent a relational database.  It will also create a strongly-typed "DataContext" class that will have properties that represent each Table we modeled within the database, as well as methods for each Stored Procedure we modeled.  As I described in &lt;a href="http://weblogs.asp.net/scottgu/archive/2007/05/19/using-linq-to-sql-part-1.aspx" target="_blank"&gt;Part 1&lt;/a&gt; of this blog post series, the DataContext class is the main conduit by which we'll query entities from the database as well as apply changes back to it.&lt;/p&gt; &lt;p&gt;Below is a screen-shot of an empty LINQ to SQL ORM designer surface, and is what you'll see immediately after creating a new LINQ to SQL data model:&lt;/p&gt; &lt;p&gt;&lt;img src="http://www.scottgu.com/blogposts/linqtosql2/step2.jpg" /&gt; &lt;/p&gt; &lt;h3&gt;&lt;u&gt;Entity Classes&lt;/u&gt;&lt;/h3&gt; &lt;p&gt;LINQ to SQL enables you to model classes that map to/from a database.  These classes are typically referred to as "Entity Classes" and instances of them are called "Entities".  Entity classes map to tables within a database.  The properties of entity classes typically map to the table's columns.  Each instance of an entity class then represents a row within the database table.&lt;/p&gt; &lt;p&gt;Entity classes defined with LINQ to SQL &lt;u&gt;do not&lt;/u&gt; have to derive from a specific base class, which means that you can have them inherit from any object you want.  All classes created using the LINQ to SQL designer are defined as "partial classes" - which means that you can optionally drop into code and add additional properties, methods and events to them.&lt;/p&gt; &lt;p&gt;Unlike the DataSet/TableAdapter feature provided in VS 2005, when using the LINQ to SQL designer you do not have to specify the SQL queries to use when creating your data model and access layer.  &lt;/p&gt; &lt;p&gt;Instead, you focus on defining your entity classes, how they map to/from the database, and the relationships between them.  The LINQ to SQL OR/M implementation will then take care of generating the appropriate SQL execution logic for you at runtime when you interact and use the data entities.  You can use LINQ query syntax to expressively indicate how to query your data model in a strongly typed way.&lt;/p&gt; &lt;h3&gt;&lt;u&gt;Creating Entity Classes From a Database&lt;/u&gt;&lt;/h3&gt; &lt;p&gt;If you already have a database schema defined, you can use it to quickly create LINQ to SQL entity classes modeled off of it.&lt;/p&gt; &lt;p&gt;The easiest way to accomplish this is to open up a database in the Server Explorer within Visual Studio, select the Tables and Views you want to model in it, and drag/drop them onto the LINQ to SQL designer surface:&lt;/p&gt; &lt;p&gt;&lt;img src="http://www.scottgu.com/blogposts/linqtosql2/step5.jpg" /&gt; &lt;/p&gt; &lt;p&gt;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 the following three entity classes created for you based on the database schema:&lt;/p&gt; &lt;p&gt;&lt;img src="http://www.scottgu.com/blogposts/linqtosql2/step6.jpg" /&gt; &lt;/p&gt; &lt;p&gt;&lt;/p&gt; &lt;p&gt;Using the data model classes defined above, I can now run all of the code samples (expect the SPROC one) described in &lt;a href="http://weblogs.asp.net/scottgu/archive/2007/05/19/using-linq-to-sql-part-1.aspx" target="_blank"&gt;Part 1&lt;/a&gt; of this LINQ to SQL series.  I don't need to add any additional code or configuration in order to enable these query, insert, update, delete, and server-side paging scenarios.  &lt;/p&gt; &lt;h3&gt;&lt;u&gt;Naming and Pluralization&lt;/u&gt;&lt;/h3&gt; &lt;p&gt;One 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 your database 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 naming helps make your models consistent with the .NET naming conventions, and I usually find having the designer fix these up for me really convenient (especially when adding lots of tables to your model).   &lt;/p&gt; &lt;p&gt;If you don't like the name of a class or property that the designer generates, though, you can always override it and change it to any name you want.  You can do this either by editing the entity/property name in-line within the designer or by modifying it via the property grid:&lt;/p&gt; &lt;p&gt;&lt;img src="http://www.scottgu.com/blogposts/linqtosql2/step7.jpg" /&gt; &lt;/p&gt; &lt;p&gt;The ability to have entity/property/association names be different from your database schema ends up being very useful in a number of cases.  In particular:&lt;/p&gt; &lt;p&gt;1) When your backend database table/column schema names change.  Because your entity models can have different names from the backend schema, you can decide to just update your mapping rules and not update your application or query code to use the new table/column name.&lt;/p&gt; &lt;p&gt;2) When you have database schema names that aren't very "clean".  For example, rather than use "au_lname" and "au_fname" for the property names on an entity class, you can just name them to "LastName" and "FirstName" on your entity class and develop against that instead (without having to rename the column names in the database).&lt;/p&gt; &lt;h3&gt;&lt;u&gt;Relationship Associations&lt;/u&gt;&lt;/h3&gt; &lt;p&gt;When you drag objects from the server explorer onto the LINQ to SQL designer, Visual Studio will inspect the primary key/foreign key relationships of the objects, and based on them automatically create default "relationship associations" between the different entity classes it creates.  For example, when I added both the Products and Categories tables from Northwind onto my LINQ to SQL designer you can see that a one to many relationship between the two is inferred (this is denoted by the arrow in the designer):&lt;/p&gt; &lt;p&gt;&lt;img src="http://www.scottgu.com/blogposts/linqtosql2/step13.jpg" /&gt; &lt;/p&gt; &lt;p&gt;The above association will cause cause the Product entity class to have a "Category" property that developers can use to access the Category entity for a given Product.  It will also cause the Category class to have a "Products" collection that enables developers to retrieve all products within that Category.&lt;/p&gt; &lt;p&gt;&lt;img src="http://www.scottgu.com/blogposts/linqtosql2/step9.jpg" /&gt; &lt;/p&gt; &lt;p&gt;If you don't like how the designer has modeled or named an association, you can always override it.  Just click on the association arrow within the designer and access its properties via the property grid to rename, delete or modify it.&lt;/p&gt; &lt;h3&gt;&lt;u&gt;Delay/Lazy Loading&lt;/u&gt;&lt;/h3&gt; &lt;p&gt;LINQ to SQL enables developers to specify whether the properties on entities should be prefetched or delay/lazy-loaded on first access.  You can customize the default pre-fetch/delay-load rules for entity properties by selecting any entity property or association in the designer, and then within the property-grid set the "Delay Loaded" property to true or false.&lt;/p&gt; &lt;p&gt;For a simple example of when I'd want to-do this, consider the "Category" entity class we modeled above.  The categories table inside "Northwind" has a "Picture" column which stores a (potentially large) binary image of each category, and I only want to retrieve the binary image from the database when I'm actually using it (and not when doing a simply query just to list the category names in a list).&lt;/p&gt; &lt;p&gt;I could configure the Picture property to be delay loaded by selecting it within the LINQ to SQL designer and by settings its Delay Loaded value in the property grid:&lt;/p&gt; &lt;p&gt;&lt;img src="http://www.scottgu.com/blogposts/linqtosql2/step8.jpg" /&gt; &lt;/p&gt; &lt;p&gt;Note: In addition to configuring the default pre-fetch/delay load semantics on entities, you can also override them via code when you perform LINQ queries on the entity class (I'll show how to-do this in the next blog post in this series).&lt;/p&gt; &lt;h3&gt;&lt;u&gt;Using Stored Procedures&lt;/u&gt;&lt;/h3&gt; &lt;p&gt;LINQ to SQL allows you to optionally model stored procedures as methods on your DataContext class.  For example, assume we've defined the simple SPROC below to retrieve product information based on a categoryID:&lt;/p&gt; &lt;p&gt;&lt;img src="http://www.scottgu.com/blogposts/linqtosql2/step10.jpg" /&gt; &lt;/p&gt; &lt;p&gt;I can use the server explorer within Visual Studio to drag/drop the SPROC onto the LINQ to SQL designer surface in order to add a strongly-typed method that will invoke the SPROC.  If I drop the SPROC on top of the "Product" entity in the designer, the LINQ to SQL designer will declare the SPROC to return an IEnumerable&amp;lt;Product&amp;gt; result:&lt;/p&gt; &lt;p&gt;&lt;img src="http://www.scottgu.com/blogposts/linqtosql2/step11.jpg" /&gt; &lt;/p&gt; &lt;p&gt;I can then use either LINQ Query Syntax (which will generate an adhoc SQL query) or alternatively invoke the SPROC method added above to retrieve product entities from the database:&lt;/p&gt; &lt;p&gt;&lt;img src="http://www.scottgu.com/blogposts/linqtosql2/step12.jpg" /&gt; &lt;/p&gt; &lt;p&gt;&lt;u&gt;Using SPROCs to Update/Delete/Insert Data&lt;/u&gt;&lt;/p&gt; &lt;p&gt;By default LINQ to SQL will automatically create the appropriate SQL expressions for you when you insert/update/delete entities.  For example, if you wrote the LINQ to SQL code below to update some values on a "Product" entity instance:&lt;/p&gt; &lt;p&gt;&lt;img src="http://www.scottgu.com/blogposts/linqtosql/step5.jpg" /&gt; &lt;/p&gt; &lt;p&gt;By default LINQ to SQL would create and execute the appropriate "UPDATE" statement for you when you submitted the changes (I'll cover this more in a later blog post on updates).&lt;/p&gt; &lt;p&gt;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 its property-grid click the "..." button on the Delete/Insert/Update values, and pick a particular SPROC you've defined instead: &lt;/p&gt; &lt;p&gt;&lt;img src="http://www.scottgu.com/blogposts/linqtosql2/step14.jpg" /&gt; &lt;/p&gt; &lt;p&gt;What is nice about changing the above setting is that it is done purely at the mapping layer of LINQ to SQL - which means the update code I showed earlier continues to work with no modifications required.  This avoids developers using a LINQ to SQL data model from having to change code even if they later decide to put in a custom SPROC optimization later.&lt;/p&gt; &lt;h3&gt;&lt;u&gt;Summary&lt;/u&gt;&lt;/h3&gt; &lt;p&gt;LINQ to SQL provides a nice, clean way to model the data layer of your application.  Once you've defined your data model you can easily and efficiently perform queries, inserts, updates and deletes against it.  &lt;/p&gt; &lt;p&gt;Using the built-in LINQ to SQL designer within Visual Studio and Visual Web Developer Express you can create and manage your data models for LINQ to SQL extremely fast.  The LINQ to SQL designer also provides a lot of flexibility that enables you to customize the default behavior and override/extend the system to meet your specific needs.&lt;/p&gt; &lt;p&gt;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 add custom business/data validation logic to the entities we designed above to perform additional validation logic.&lt;/p&gt; &lt;p&gt;Mike Taulty also has a number of great LINQ to SQL videos that I recommend checking out &lt;a href="http://mtaulty.com/CommunityServer/blogs/mike_taultys_blog/archive/2007/05/10/9322.aspx" target="_blank"&gt;here&lt;/a&gt;.  These provide a great way to learn by watching someone walkthrough using LINQ to SQL in action.&lt;/p&gt; &lt;p&gt;Hope this helps,&lt;/p&gt; &lt;p&gt;Scott&lt;/p&gt;&lt;/font&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=2681237" width="1" height="1" /&gt;</description>
    </item>
    <item>
      <title>LINQ to SQL (Part 3 - Querying our Database)</title>
      <pubDate>Sat, 30 Jun 2007 16:10:18 +0100</pubDate>
      <guid isPermaLink="false">49e168f1-b7c9-8c59-7d57-0ad92ad14749</guid>
      <description>&lt;a href="http://weblogs.asp.net/scottgu/archive/2007/06/29/linq-to-sql-part-3-querying-our-database.aspx"&gt;Linked article&lt;/a&gt; originally from &lt;a href="http://weblogs.asp.net/scottgu/default.aspx"&gt;ScottGu's Blog&lt;/a&gt;&lt;br /&gt;&lt;div&gt;&lt;/div&gt;&lt;br /&gt;&lt;font face="arial" size="2"&gt; &lt;p&gt;Last month I started a blog post series covering LINQ to SQL.  LINQ to SQL is a built-in O/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 then use LINQ expressions to query the database with them, as well as update/insert/delete data from it.&lt;/p&gt; &lt;p&gt;Below are the first two parts of my LINQ to SQL series:&lt;/p&gt; &lt;ul&gt; &lt;li&gt;&lt;a href="http://weblogs.asp.net/scottgu/archive/2007/05/19/using-linq-to-sql-part-1.aspx" target="_blank"&gt;Part 1: Introduction to LINQ to SQL&lt;/a&gt;  &lt;li&gt;&lt;a href="http://weblogs.asp.net/scottgu/archive/2007/05/29/linq-to-sql-part-2-defining-our-data-model-classes.aspx" target="_blank"&gt;Part 2: Defining our Data Model Classes&lt;/a&gt;&lt;/li&gt;&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;In today's blog post I'll be going into more detail on how to use the data model we created in the &lt;a href="http://weblogs.asp.net/scottgu/archive/2007/05/29/linq-to-sql-part-2-defining-our-data-model-classes.aspx" target="_blank"&gt;Part 2&lt;/a&gt; post, and show how to use it to query data within an ASP.NET project.&lt;/p&gt; &lt;h3&gt;&lt;u&gt;Northwind Database Modeled using LINQ to SQL&lt;/u&gt;&lt;/h3&gt; &lt;p&gt;In &lt;a href="http://weblogs.asp.net/scottgu/archive/2007/05/29/linq-to-sql-part-2-defining-our-data-model-classes.aspx" target="_blank"&gt;Part 2&lt;/a&gt; of this series I walked through how to create a LINQ to SQL class model using the LINQ to SQL designer that is built-into VS 2008.  Below is the class model that we created for the Northwind sample database:&lt;/p&gt; &lt;p&gt;&lt;img src="http://www.scottgu.com/blogposts/linqtosql/step1.jpg" /&gt; &lt;/p&gt; &lt;h3&gt;&lt;u&gt;Retrieving Products&lt;/u&gt;&lt;/h3&gt; &lt;p&gt;Once we have defined our data model classes above, we can easily query and retrieve data from our database.  LINQ to SQL enables you to do this by writing LINQ syntax queries against the NorthwindDataContext class that we created using the LINQ to SQL designer above.&lt;/p&gt; &lt;p&gt;For example, to retrieve and iterate over a sequence of Product objects I could write code like below:&lt;/p&gt; &lt;p&gt;&lt;img src="http://www.scottgu.com/blogposts/linqtosql3/step1.jpg" /&gt; &lt;/p&gt; &lt;p&gt;In the query above I have used a "where" clause in my LINQ syntax query to only return those products within a specific category.  I am using the CategoryID of the Product to perform the filter.&lt;/p&gt; &lt;p&gt;One of the nice things above LINQ to SQL is that I have a lot of flexibility in how I query my data, and I can take advantage of the associations I've setup when modeling my LINQ to SQL data classes to perform richer and more natural queries against the database.  For example, I could modify the query to filter by the product's CategoryName instead of its CategoryID by writing my LINQ query like so:&lt;/p&gt; &lt;p&gt;&lt;img src="http://www.scottgu.com/blogposts/linqtosql3/step2.jpg" /&gt; &lt;/p&gt; &lt;p&gt;Notice above how I'm using the "Category" property that is on each of the Product objects to filter by the CategoryName of the Category that the Product belongs to.  This property was automatically created for us by LINQ to SQL because we modeled the Category and Product classes as having a many to one relationship with each other in the database.&lt;/p&gt; &lt;p&gt;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 more orders placed for them:&lt;/p&gt; &lt;p&gt;&lt;img src="http://www.scottgu.com/blogposts/linqtosql3/step3.jpg" /&gt; &lt;/p&gt; &lt;p&gt;Notice above how we are using the "OrderDetails" collection that LINQ to SQL has created for us on each Product class (because of the 1 to many relationship we modeled in the LINQ to SQL designer).&lt;/p&gt; &lt;h3&gt;&lt;u&gt;Visualizing LINQ to SQL Queries in the Debugger&lt;/u&gt;&lt;/h3&gt; &lt;p&gt;Object relational mappers like LINQ to SQL handle automatically creating and executing the appropriate SQL code for you when you perform a query or update against their object model.  &lt;/p&gt; &lt;p&gt;One of the biggest concerns/fears that developers new to ORMs have is "but what SQL code is it actually executing?"  One of the really nice things about LINQ to SQL is that it makes it super easy to see &lt;em&gt;exactly&lt;/em&gt; what SQL code it is executing when you run your application within the debugger.&lt;/p&gt; &lt;p&gt;Starting with Beta2 of Visual Studio 2008 you can use a new LINQ to SQL visualizer plug-in to easily see (and test out) any LINQ to SQL query expression.  Simply set a breakpoint and then hover over a LINQ to SQL query and click the magnify glass to pull up its expression visualizer within the debugger:&lt;/p&gt; &lt;p&gt;&lt;img src="http://www.scottgu.com/blogposts/linqtosql3/step4.jpg" /&gt; &lt;/p&gt; &lt;p&gt;This will then bring up a dialog that shows you the exact SQL that LINQ to SQL will use when executing the query to retrieve the Product objects:&lt;/p&gt; &lt;p&gt;&lt;img src="http://www.scottgu.com/blogposts/linqtosql3/step5.jpg" /&gt; &lt;/p&gt; &lt;p&gt;If you press the "Execute" button within this dialog it will allow you to evaluate the SQL directly within the debugger and see the exact data results returned from the database:&lt;/p&gt; &lt;p&gt;&lt;img src="http://www.scottgu.com/blogposts/linqtosql3/step6.jpg" /&gt; &lt;/p&gt; &lt;p&gt;This obviously makes it super easy to see precisely what SQL query logic LINQ to SQL is doing for you.  Note that you can optionally override the raw SQL that LINQ to SQL executes in cases where you want to change it - although in 98% of scenarios I think you'll find that the SQL code that LINQ to SQL executes is really, really good.&lt;/p&gt; &lt;h3&gt;&lt;u&gt;Databinding LINQ to SQL Queries to ASP.NET Controls&lt;/u&gt;&lt;/h3&gt; &lt;p&gt;LINQ queries return results that implement the IEnumerable interface - which is also an interface 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 any ASP.NET control.&lt;/p&gt; &lt;p&gt;For example, we could declare an &amp;lt;asp:gridview&amp;gt; control in a .aspx page like so:&lt;/p&gt; &lt;p&gt;&lt;img src="http://www.scottgu.com/blogposts/linqtosql3/step8.jpg" /&gt; &lt;/p&gt; &lt;p&gt;I could then databind the result of the LINQ to SQL query we wrote before to the GridView like so:&lt;/p&gt; &lt;p&gt;&lt;img src="http://www.scottgu.com/blogposts/linqtosql3/step7.jpg" /&gt; &lt;/p&gt; &lt;p&gt;This will then generate a page that looks like below:&lt;/p&gt; &lt;p&gt;&lt;img src="http://www.scottgu.com/blogposts/linqtosql3/step9.jpg" /&gt; &lt;/p&gt; &lt;h3&gt;&lt;u&gt;Shaping our Query Results&lt;/u&gt;&lt;/h3&gt; &lt;p&gt;Right now when we are evaluating our product query, we are retrieving by default all of the column data needed to populate the Product entity classes.  &lt;/p&gt; &lt;p&gt;For example, this query to retrieve products:&lt;/p&gt; &lt;p&gt;&lt;img src="http://www.scottgu.com/blogposts/linqtosql3/step2.jpg" /&gt; &lt;/p&gt; &lt;p&gt;Results in all of this data being returned:&lt;/p&gt; &lt;p&gt;&lt;img src="http://www.scottgu.com/blogposts/linqtosql3/step6.jpg" /&gt; &lt;/p&gt; &lt;p&gt;Often we only want to return a subset of the data about each product.  We can use the new &lt;a href="http://weblogs.asp.net/scottgu/archive/2007/04/21/new-orcas-language-feature-query-syntax.aspx" target="_blank"&gt;data shaping features&lt;/a&gt; that LINQ and the new C# and VB compilers support to indicate that we only want a subset of the data by modifying our LINQ to SQL query like so:&lt;/p&gt; &lt;p&gt;&lt;img src="http://www.scottgu.com/blogposts/linqtosql3/step10.jpg" /&gt; &lt;/p&gt; &lt;p&gt;This will result in only this data subset being returned from our database (as seen via our debug visualizer):&lt;/p&gt; &lt;p&gt;&lt;img src="http://www.scottgu.com/blogposts/linqtosql3/step11.jpg" /&gt; &lt;/p&gt; &lt;p&gt;What is cool about LINQ to SQL is that I can take full advantage of my data model class associations when shaping my data.  This enables me to express really useful (and very efficient) data queries.  For example, the below query retrieves the ID and Name from the Product entity, the total number of orders that have been made for the Product, and then sums up the total revenue value of each of the Product's orders:&lt;/p&gt; &lt;p&gt;&lt;img src="http://www.scottgu.com/blogposts/linqtosql3/step12.jpg" /&gt; &lt;/p&gt; &lt;p&gt;The expression to the right of the "Revenue" property above is an example of using the "Sum" &lt;a href="http://weblogs.asp.net/scottgu/archive/2007/03/13/new-orcas-language-feature-extension-methods.aspx" target="_blank"&gt;extension method&lt;/a&gt; provided by LINQ.  It takes a &lt;a href="http://weblogs.asp.net/scottgu/archive/2007/04/08/new-orcas-language-feature-lambda-expressions.aspx" target="_blank"&gt;Lambda expression&lt;/a&gt; that returns the value of each product order item as an argument.  &lt;/p&gt; &lt;p&gt;LINQ to SQL is smart and is able to convert the above LINQ expression to the below SQL when it is evaluated (as seen via our debug visualizer):&lt;/p&gt; &lt;p&gt;&lt;img src="http://www.scottgu.com/blogposts/linqtosql3/step13.jpg" /&gt; &lt;/p&gt; &lt;p&gt;The above SQL causes all of the NumOrders and Revenue value computations to be done inside the SQL server, and results in only the below data being retrieved from the database (making it really fast):&lt;/p&gt; &lt;p&gt;&lt;img src="http://www.scottgu.com/blogposts/linqtosql3/step14.jpg" /&gt; &lt;/p&gt; &lt;p&gt;We can then databind the result sequence against our GridView control to generate pretty UI:&lt;/p&gt; &lt;p&gt;&lt;img src="http://www.scottgu.com/blogposts/linqtosql3/step21.jpg" /&gt; &lt;/p&gt; &lt;p&gt;BTW - in case you were wondering, you do get full intellisense within VS 2008 when writing these types of LINQ shaping queries:&lt;/p&gt; &lt;p&gt;&lt;img src="http://www.scottgu.com/blogposts/linqtosql3/step15.jpg" /&gt; &lt;/p&gt; &lt;p&gt;In the example above I'm declaring an &lt;a href="http://weblogs.asp.net/scottgu/archive/2007/05/15/new-orcas-language-feature-anonymous-types.aspx?CommentPosted=true" target="_blank"&gt;anonymous type&lt;/a&gt; that uses &lt;a href="http://weblogs.asp.net/scottgu/archive/2007/03/08/new-c-orcas-language-features-automatic-properties-object-initializers-and-collection-initializers.aspx" target="_blank"&gt;object initialization&lt;/a&gt; to shape and 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 result sequences as well:&lt;/p&gt; &lt;p&gt; &lt;img src="http://www.scottgu.com/blogposts/linqtosql3/step16.jpg" /&gt; &lt;/p&gt; &lt;h3&gt;&lt;u&gt;Paging our Query Results&lt;/u&gt;&lt;/h3&gt; &lt;p&gt;One 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 easy and efficient - the Skip() and Take() methods.&lt;/p&gt; &lt;p&gt;We can use the Skip() and Take() methods below to indicate that we only want to return 10 product objects - starting at an initial product row that we specify as a parameter argument:&lt;/p&gt; &lt;p&gt;&lt;img src="http://www.scottgu.com/blogposts/linqtosql3/step17.jpg" /&gt; &lt;/p&gt; &lt;p&gt;Note above how I did not add the Skip() and Take() operator on the initial products query declaration - but instead added it later to the query (when binding it to my GridView datasource).  People often ask me "but doesn't this mean that the query first grabs all the data from the database and then does the paging in the middle tier (which is bad)?"  No.  The reason is that LINQ uses a deferred execution model - which means that the query doesn't actually execute until you try and iterate over the results.  &lt;/p&gt; &lt;p&gt;One of the benefits of this deferred execution model is that it enables you to nicely compose queries across multiple code statements (which improves code readability).  It also enables you to compose queries out of other queries - which enables some very flexible query composition and re-use scenarios.&lt;/p&gt; &lt;p&gt;Once I have the BindProduct() method defined above, I can write the code below in my page to retrieve the starting index from the querystring and cause the products to be paged and displayed in the gridview:&lt;/p&gt; &lt;p&gt;&lt;img src="http://www.scottgu.com/blogposts/linqtosql3/step18.jpg" /&gt; &lt;/p&gt; &lt;p&gt;This will then give us a products page, filtered to list only those products with more than 5 orders, showing dynamically computed product data, and which is pageable via a querystring argument:&lt;/p&gt; &lt;p&gt;&lt;img src="http://www.scottgu.com/blogposts/linqtosql3/step19.jpg" /&gt; &lt;/p&gt; &lt;p&gt;Note: When working against SQL 2005, LINQ to SQL will use the ROW_NUMBER() SQL function to perform all of the data paging logic in the database.  This ensures that only the 10 rows of data we want in the current page view are returned from the database when we execute the above code:&lt;/p&gt; &lt;p&gt;&lt;img src="http://www.scottgu.com/blogposts/linqtosql3/step20.jpg" /&gt; &lt;/p&gt; &lt;p&gt;This makes it efficient and easy to page over large data sequences.&lt;/p&gt; &lt;h3&gt;&lt;u&gt;Summary&lt;/u&gt;&lt;/h3&gt; &lt;p&gt;Hopefully the above walkthrough provides a good overview of some of the cool data query opportunities that LINQ to SQL provides.  To learn more about LINQ expressions and the new language syntax supported by the C# and VB compilers with VS 2008, please read these earlier posts of mine:&lt;/p&gt; &lt;ul&gt; &lt;li&gt;&lt;a href="http://weblogs.asp.net/scottgu/archive/2007/03/08/new-c-orcas-language-features-automatic-properties-object-initializers-and-collection-initializers.aspx" target="_blank"&gt;Automatic Properties, Object Initializer and Collection Initializers&lt;/a&gt;  &lt;li&gt;&lt;a href="http://weblogs.asp.net/scottgu/archive/2007/03/13/new-orcas-language-feature-extension-methods.aspx" target="_blank"&gt;Extension Methods&lt;/a&gt;  &lt;li&gt;&lt;a href="http://weblogs.asp.net/scottgu/archive/2007/04/08/new-orcas-language-feature-lambda-expressions.aspx" target="_blank"&gt;Lambda Expressions&lt;/a&gt;  &lt;li&gt;&lt;a href="http://weblogs.asp.net/scottgu/archive/2007/04/21/new-orcas-language-feature-query-syntax.aspx" target="_blank"&gt;Query Syntax&lt;/a&gt;  &lt;li&gt;&lt;a href="http://weblogs.asp.net/scottgu/archive/2007/05/15/new-orcas-language-feature-anonymous-types.aspx?CommentPosted=true" target="_blank"&gt;Anonymous Types&lt;/a&gt; &lt;/li&gt;&lt;/li&gt;&lt;/li&gt;&lt;/li&gt;&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;In my next post in this LINQ to SQL series I'll cover how we can cleanly add validation logic to our data model classes, and demonstrate how we can use it to encapsulate business logic that executes every time we update, insert, or delete our data.  I'll then cover more advanced lazy and eager loading query scenarios, how to use the new &amp;lt;asp:LINQDataSource&amp;gt; control to support declarative databinding of ASP.NET controls, optimistic concurrency error resolution, and more.&lt;/p&gt; &lt;p&gt;Hope this helps,&lt;/p&gt; &lt;p&gt;Scott&lt;/p&gt;&lt;/font&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=2973713" width="1" height="1" /&gt;</description>
    </item>
    <item>
      <title>Using LINQ to SQL (Part 1)</title>
      <pubDate>Tue, 22 May 2007 05:41:08 +0100</pubDate>
      <guid isPermaLink="false">3b99769f-a1cc-3aee-0377-b016ee13c89b</guid>
      <description>&lt;a href="http://weblogs.asp.net/scottgu/archive/2007/05/19/using-linq-to-sql-part-1.aspx"&gt;Linked article&lt;/a&gt; originally from &lt;a href="http://weblogs.asp.net/scottgu/default.aspx"&gt;ScottGu's Blog&lt;/a&gt;&lt;br /&gt;&lt;div&gt;&lt;/div&gt;&lt;br /&gt;&lt;font face="arial" size="2"&gt; &lt;p&gt;Over the last few months I wrote a series of blog posts that covered some of the new language features that are coming with the Visual Studio and .NET Framework "Orcas" release.  Here are pointers to the posts in my series:  &lt;ul&gt; &lt;li&gt;&lt;a href="http://weblogs.asp.net/scottgu/archive/2007/03/08/new-c-orcas-language-features-automatic-properties-object-initializers-and-collection-initializers.aspx" target="_blank"&gt;Automatic Properties, Object Initializer and Collection Initializers&lt;/a&gt;  &lt;li&gt;&lt;a href="http://weblogs.asp.net/scottgu/archive/2007/03/13/new-orcas-language-feature-extension-methods.aspx" target="_blank"&gt;Extension Methods&lt;/a&gt;  &lt;li&gt;&lt;a href="http://weblogs.asp.net/scottgu/archive/2007/04/08/new-orcas-language-feature-lambda-expressions.aspx" target="_blank"&gt;Lambda Expressions&lt;/a&gt;  &lt;li&gt;&lt;a href="http://weblogs.asp.net/scottgu/archive/2007/04/21/new-orcas-language-feature-query-syntax.aspx" target="_blank"&gt;Query Syntax&lt;/a&gt;  &lt;li&gt;&lt;a href="http://weblogs.asp.net/scottgu/archive/2007/05/15/new-orcas-language-feature-anonymous-types.aspx?CommentPosted=true" target="_blank"&gt;Anonymous Types&lt;/a&gt;&lt;/li&gt;&lt;/li&gt;&lt;/li&gt;&lt;/li&gt;&lt;/li&gt;&lt;/ul&gt; &lt;/p&gt;&lt;p&gt;The above language features help make &lt;em&gt;querying data &lt;/em&gt;a first class programming concept.  We call this overall querying programming model "LINQ" - which stands for &lt;em&gt;.NET Language Integrated Query&lt;/em&gt;.  &lt;/p&gt;&lt;p&gt;Developers can use LINQ with any data source.  They can express efficient query behavior in their programming language of choice, optionally transform/shape data query results into whatever format they want, and then easily manipulate the results.  LINQ-enabled languages can provide full type-safety and compile-time checking of query expressions, and development tools can provide full intellisense, debugging, and rich refactoring support when writing LINQ code.  &lt;/p&gt;&lt;p&gt;LINQ supports a very rich extensibility model that facilitates the creation of very efficient domain-specific operators for data sources.  The "Orcas" version of the .NET Framework ships with built-in libraries that enable LINQ support against Objects, XML, and Databases.  &lt;h3&gt;&lt;u&gt;What Is LINQ to SQL?&lt;/u&gt;&lt;/h3&gt; &lt;/p&gt;&lt;p&gt;LINQ to SQL is an &lt;a href="http://en.wikipedia.org/wiki/Object-relational_mapping" target="_blank"&gt;O/RM&lt;/a&gt; (object relational mapping) implementation that ships in the .NET Framework "Orcas" release, and which allows you to model a relational database using .NET classes.  You can then query the database using LINQ, as well as update/insert/delete data from it.&lt;/p&gt; &lt;p&gt;LINQ to SQL fully supports transactions, views, and stored procedures.  It also provides an easy way to integrate data validation and business logic rules into your data model.&lt;/p&gt; &lt;h3&gt;&lt;u&gt;Modeling Databases Using LINQ to SQL:&lt;/u&gt;&lt;/h3&gt; &lt;p&gt;Visual Studio "Orcas" ships with a LINQ to SQL designer that provides an easy way to model and visualize a database as a LINQ to SQL object model.  My next blog post will cover in more depth how to use this designer (you can also &lt;a href="http://weblogs.asp.net/scottgu/archive/2007/01/28/video-using-linq-with-asp-net-in-vs-orcas-part-1.aspx" target="_blank"&gt;watch this video&lt;/a&gt; I made in January to see me build a LINQ to SQL model from scratch using it).  &lt;/p&gt; &lt;p&gt;Using the LINQ to SQL designer I can easily create a representation of the sample "Northwind" database like below:&lt;/p&gt; &lt;p&gt;&lt;img src="http://www.scottgu.com/blogposts/linqtosql/step1.jpg" /&gt; &lt;/p&gt; &lt;p&gt;My LINQ to SQL design-surface above defines four entity classes: Product, Category, Order and OrderDetail.  The properties of each class map to the columns of a corresponding table in the database.  Each instance of a class entity represents a row within the database table.&lt;/p&gt; &lt;p&gt;The arrows between the four entity classes above represent associations/relationships between the different entities.  These are typically modeled using primary-key/foreign-key relationships in the database.  The direction of the arrows on the design-surface indicate whether the association is a one-to-one or one-to-many relationship.  Strongly-typed properties will be added to the entity classes based on this.  For example, the Category class above 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.  The Product class then has a "Category" property that points to a Category class instance that represents the Category to which the Product belongs.&lt;/p&gt; &lt;p&gt;The right-hand method pane within the LINQ to SQL design surface above contains a list of stored 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 a sequence of Product entities as a result.  We'll look at how to call this SPROC in a code sample below.&lt;/p&gt; &lt;p&gt;&lt;u&gt;Understanding the DataContext Class&lt;/u&gt;&lt;/p&gt; &lt;p&gt;When you press the "save" button within the LINQ to SQL designer surface, Visual Studio will 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 DataContext class will also be generated.  This DataContext class is the main conduit by which we'll query entities from the database as well as apply changes.  The DataContext class created will have properties that represent each Table we modeled within the database, as well as methods for each Stored Procedure we added.&lt;/p&gt; &lt;p&gt;For example, below is the NorthwindDataContext class that is persisted based on the model we designed above:&lt;/p&gt; &lt;p&gt;&lt;img src="http://www.scottgu.com/blogposts/linqtosql/step2.jpg" /&gt;&lt;/p&gt; &lt;h3&gt;&lt;u&gt;LINQ to SQL Code Examples&lt;/u&gt;&lt;/h3&gt; &lt;p&gt;Once we've modeled our database using the LINQ to SQL designer, we can then easily write code to work against it.  Below are a few code examples that show off common data tasks:&lt;/p&gt; &lt;h4&gt;&lt;u&gt;1) Query Products From the Database&lt;/u&gt;&lt;/h4&gt; &lt;p&gt;The code below uses LINQ query syntax to retrieve an IEnumerable sequence of Product objects.  Note how the code is querying across the Product/Category relationship to only retrieve those products in the "Beverages" category:&lt;/p&gt; &lt;p&gt;&lt;em&gt;C#:&lt;/em&gt;&lt;/p&gt; &lt;p&gt;&lt;img src="http://www.scottgu.com/blogposts/linqtosql/step3.jpg" /&gt; &lt;/p&gt; &lt;p&gt;&lt;em&gt;VB:&lt;/em&gt;&lt;/p&gt; &lt;p&gt;&lt;img src="http://www.scottgu.com/blogposts/linqtosql/step9.jpg" /&gt; &lt;/p&gt; &lt;h4&gt;&lt;u&gt;2) Update a Product in the Database&lt;/u&gt;&lt;/h4&gt; &lt;p&gt;The code below demonstrates how to retrieve a single product from the database, update its price, and then save the changes back to the database:&lt;/p&gt; &lt;p&gt;&lt;em&gt;C#:&lt;/em&gt;&lt;/p&gt; &lt;p&gt;&lt;img src="http://www.scottgu.com/blogposts/linqtosql/step5.jpg" /&gt; &lt;/p&gt; &lt;p&gt;&lt;em&gt;VB:&lt;/em&gt;&lt;/p&gt; &lt;p&gt;&lt;img src="http://www.scottgu.com/blogposts/linqtosql/step11.jpg" /&gt; &lt;/p&gt; &lt;p&gt;&lt;em&gt;Note: VB in "Orcas" Beta1 doesn't support Lambdas yet.  It will, though, in Beta2 - at which point the above query can be rewritten to be more concise.&lt;/em&gt;&lt;/p&gt; &lt;h4&gt;&lt;u&gt;3) Insert a New Category and Two New Products into the Database&lt;/u&gt;&lt;/h4&gt; &lt;p&gt;The code below demonstrates how to create a new category, and then create two new products and associate them with the category.  All three are then saved into the database.&lt;/p&gt; &lt;p&gt;Note below how I don't need to manually manage the primary key/foreign key relationships. 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/FK relationships for me.  &lt;/p&gt; &lt;p&gt;&lt;em&gt;C# &lt;/em&gt;&lt;/p&gt; &lt;p&gt;&lt;img src="http://www.scottgu.com/blogposts/linqtosql/step4.jpg" /&gt; &lt;/p&gt; &lt;p&gt;&lt;em&gt;VB:&lt;/em&gt;&lt;/p&gt; &lt;p&gt;&lt;img src="http://www.scottgu.com/blogposts/linqtosql/step12.jpg" /&gt; &lt;/p&gt; &lt;h4&gt;&lt;u&gt;4) Delete Products from the Database&lt;/u&gt;&lt;/h4&gt; &lt;p&gt;The code below demonstrates how to delete all Toy products from the database:&lt;/p&gt; &lt;p&gt;&lt;em&gt;C#:&lt;/em&gt;&lt;/p&gt; &lt;p&gt;&lt;img src="http://www.scottgu.com/blogposts/linqtosql/step6.jpg" /&gt; &lt;/p&gt; &lt;p&gt;&lt;em&gt;VB:&lt;/em&gt;&lt;/p&gt; &lt;p&gt;&lt;img src="http://www.scottgu.com/blogposts/linqtosql/step13.jpg" /&gt; &lt;/p&gt; &lt;h4&gt;&lt;u&gt;5) Call a Stored Procedure&lt;/u&gt;&lt;/h4&gt; &lt;p&gt;The 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 data model above.  Note that once I retrieve the Product results, I can update/delete them and then call db.SubmitChanges() to persist the modifications back to the database.&lt;/p&gt; &lt;p&gt;&lt;em&gt;C#:&lt;/em&gt;&lt;/p&gt; &lt;p&gt;&lt;img src="http://www.scottgu.com/blogposts/linqtosql/step7.jpg" /&gt; &lt;/p&gt; &lt;p&gt;&lt;em&gt;VB:&lt;/em&gt;&lt;/p&gt; &lt;p&gt;&lt;img src="http://www.scottgu.com/blogposts/linqtosql/step14.jpg" /&gt; &lt;/p&gt; &lt;h4&gt;&lt;u&gt;6) Retrieve Products with Server Side Paging&lt;/u&gt;&lt;/h4&gt; &lt;p&gt;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.&lt;/p&gt; &lt;p&gt;&lt;em&gt;C#:&lt;/em&gt;&lt;/p&gt; &lt;p&gt;&lt;img src="http://www.scottgu.com/blogposts/linqtosql/step8.jpg" /&gt; &lt;/p&gt; &lt;p&gt;&lt;em&gt;VB:&lt;/em&gt;&lt;/p&gt; &lt;p&gt;&lt;img src="http://www.scottgu.com/blogposts/linqtosql/step10.jpg" /&gt; &lt;/p&gt; &lt;h3&gt;&lt;u&gt;Summary&lt;/u&gt;&lt;/h3&gt; &lt;p&gt;LINQ to SQL provides a nice, clean way to model the data layer of your application.  Once you've defined your data model you can easily and efficiently perform queries, inserts, updates and deletes against it.  &lt;/p&gt; &lt;p&gt;Hopefully the above introduction and code samples have helped whet your appetite to learn more.  Over the next few weeks I'll be continuing this series to explore LINQ to SQL in more detail.&lt;/p&gt; &lt;p&gt;Hope this helps,&lt;/p&gt; &lt;p&gt;Scott&lt;/p&gt;&lt;/font&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=2619512" width="1" height="1" /&gt;</description>
    </item>
  </channel>
</rss>