.NET
  Home arrow .NET arrow Page 2 - Introducing LINQ with XML and Databases
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
.NET

Introducing LINQ with XML and Databases
By: Peyton McCullough
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 2
    2008-06-16

    Table of Contents:
  • Introducing LINQ with XML and Databases
  • Querying the Database
  • LINQ to XML
  • XML Literals in Visual Basic

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Introducing LINQ with XML and Databases - Querying the Database


    (Page 2 of 4 )

    Now that we have everything ready, we can begin to use LINQ to query the Adventure Works database. Let's begin with an easy task: querying the database to get every customer:

    var customers = from c in db.Customers

     select c;


    This is made remarkably simple. It's just like querying the array in the last article, a similarity which is, of course, intended.

    We now have access to each customer's information. We can access each of the properties defined in the Customer class we created earlier, which correspond to the database table's fields. For example, let's display the first and last names of each customer:

    foreach (var c in customers)

    {

     Console.WriteLine("{0} {1}", c.FirstName, c.LastName);

    }


    Again, there are no surprises. LINQ behaves the same here with an SQL database as it did in the last article with an array of Person objects.

    Since, above, we only use the first and last name of the customer, we could have narrowed down the properties we obtained from the database by using an anonymous type containing only the properties for the first and last names:

    var customers = from c in db.Customers

     select new { c.FirstName, c.LastName };


    Of course, if we run the loop again, the exact same results are displayed.

    This article is not intended to be a comprehensive, in-depth guide to LINQ to SQL, but we'll look at one final thing before moving on to LINQ to XML: associations. The Adventure Works database contains a table called SalesOrderHeader. This table contains general details about sales orders. (Details about the contents of orders are stored in the SalesOrderDetails table, with one entry per type of product ordered). Notice how the table contains a CustomerID field, which, of course, corresponds to a particular entry in the Customer table. So, each entry in the SalesOrderHeader corresponds to exactly one entry in the Customer table. Given an entry in the SalesOrderHeader table, we should be able to, for example, print the name of the customer involved.

    Visual Studio makes this easy. Go to the LINQ to SQL Classes item we created earlier and drag the SalesOrderHeader onto the Object Relational Designer. A graphical representation of the table will appear, and so should an arrow between the Customer table and that SalesOrderHeader table. This arrow represents an association between the two tables. In this case, the association is built around the CustomerID field, which appears in both tables. If you double click the arrow, the Association Editor dialog will appear, showing the properties of the association. Customer is listed as the parent class, since the CustomerID is the primary key of this class, and SalesOrderHeader is listed as the child class. Visual Basic was able to automatically determine this relationship (though it is possible to manually add associations through the Designer's context menu).

    Now, let's display the order number of each order, along with the customer's name. This is actually very easy:


    var orders = from o in db.SalesOrderHeaders

     select o;


    foreach (var o in orders)

    {

     Console.WriteLine("Order Number: {0}", o.SalesOrderNumber);

     Console.WriteLine("\tCustomer: {0} {1}", o.Customer.FirstName, o.Customer.LastName);

    }



    As you can see, there is nothing special about the query itself. We simply retrieve every element in the SalesOrderHeader table. There is also nothing complex in the loop where we print out the information. The corresponding entry in the Customer table is added as a property to the SalesOrderHeader object we pulled from the database, and we access the FirstName and LastName properties of the Customer object just as we did earlier. This is a very nice capability, especially considering that all we did was drag and drop a table.

    More .NET Articles
    More By Peyton McCullough


       · Hello, all,This is a continuation of my...
     

    .NET ARTICLES

    - Using CrystalReportViewer to Display Crystal...
    - Creating Summary .Net Crystal Reports
    - More on Commands, Input and the WPF
    - Grouping and Aggregating When Querying LINQ ...
    - Commands, Input and the WPF
    - Keyboard and Ink Input with WPF
    - Mouse Input and the WPF
    - Input with Windows Presentation Foundation
    - Introducing LINQ with XML and Databases
    - An Introduction to LINQ
    - Querying LINQ to SQL: Basics
    - Completing a Simple Storefront with LINQ
    - Knowing Your Environment: the System.Environ...
    - Creating the Home Page for a Simple Storefro...
    - LINQ Quickly with Language Integrated Queries

     
    Best Practices for Windows Vista Migration Presentation
    Dell and Microsoft recently held a series of face-to-face seminars entitled, &qu....

     
    Creating a Culture for Code Reuse
    If you oversee development teams you know that like it or not proprietary and ex....

     
    Keys to Web Application Acceleration: Advances in Delivery Systems
    Accelerate Web apps by up to 5x. Ensure significantly faster access to the Web a....

     
    Optimizing Application Monitoring
    Tired of finding out from your customers that you're offline? This white paper e....

     
    Solaris to Solaris Migration -- Migrating applications from Sun SPARC to Dell PowerEdge R900
    This comprehensive Migration Guide reviews the approach that Principled Technolo....

     




    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
    Stay green...Green IT