.NET
  Home arrow .NET arrow Querying LINQ to SQL: Basics
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 
Dedicated Servers 
Actuate Whitepapers 
Moblin 
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

Querying LINQ to SQL: Basics
By: Jagadish Chaterjee
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 4
    2008-05-21

    Table of Contents:
  • Querying LINQ to SQL: Basics
  • Column aliasing and expression based columns using LINQ
  • Retrieving DISTINCT values using LINQ
  • Sorting rows with ORDER BY using LINQ
  • Filtering rows with conditions and implementing SQL IN operator using LINQ
  • Implementing LIKE and IS NULL operators using LINQ

  • 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

    Free Web 2.0 Code Generator! Generate data entry and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!

    Querying LINQ to SQL: Basics


    (Page 1 of 6 )

    This is an introductory article that focuses on basic querying using “LINQ to SQL.” It covers basic LINQ operators with both SQL and LINQ examples. If you are new to programming LINQ, please consider reading my previous articles, called “Beginning ‘LINQ to SQL’ using Visual Studio 2008” and “Introducing ‘LINQ to SQL’ designer using Visual Studio 2008.”

    For the purpose of this article, I created a LINQ model using “LINQ to SQL designer” as follows:

    Fig 01

    All the queries in this article will focus on the above model. The image is linked to here due to size and clarity considerations.

    The entire source code for this article is available in the form of a free downloadable zip file (CS and VB). The solution was developed using Microsoft Visual Studio 2008 Team Edition on Microsoft Windows Server 2003 Standard Edition with Microsoft SQL Server 2005 Developer Edition. I didn’t really test it in any other environment. I request that you post in the discussion area if you have any problems regarding execution.

    Beginning to fetch data using LINQ (i.e. fetching rows from a table)

    To begin fetching data, you must start with a query. As we already have a model created using “LINQ to SQL Designer,” we will start fetching data from that model using LINQ.

    In general, to retrieve all rows from the Regions table, we would write a SQL SELECT as follows:


    SELECT * FROM Region


    The LINQ statement (in VB) for the above SELECT can be written as follows:


    Dim q = From p In db.Regions _

    Select p


    To make it simpler, we can even omit the “Select” part as follows (only in VB):


    Dim q = From p In db.Regions


    The complete code for the above examples would be as follows:


    Protected Sub btnSelectFromRegions_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSelectFromRegions.Click

    Dim db As New NorthwindDataContext

    Dim s As New IO.StringWriter

    db.Log = s


    Dim q = From p In db.Regions

    'Dim q = From p In db.Regions _

    ' Select p

    Me.GridView1.DataSource = q

    Me.GridView1.DataBind()


    Me.lblGenSQL.Text = s.ToString

    End Sub


    This is the code in C#:


    protected void btnSelectFromRegions_Click(object sender, EventArgs e)

    {

    NorthwindDataContext db = new NorthwindDataContext();

    StringWriter s = new StringWriter();

    db.Log = s;


    var q = from p in db.Regions

    select p;

    this.GridView1.DataSource = q;

    this.GridView1.DataBind();


    this.lblGenSQL.Text = s.ToString();

    }


    Let us go further with the following SELECT query:


    SELECT RegionID, RegionDescription FROM Region


    In the above case, we are specific for only certain columns (but not all columns). The LINQ query (in VB) for the above example would be as follows:


    Dim q = From p In db.Regions _

    Select p.RegionID, p.RegionDescription


    The same in C# would be as follows:


    var q = from p in db.Regions

    select new { p.RegionID, p.RegionDescription };

    More .NET Articles
    More By Jagadish Chaterjee


       · Hai,This is the first in series focusing on "Querying using LINQ to SQL". It...
     

    .NET ARTICLES

    - 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
    - Introducing LINQ to SQL Designer using Visua...
    - Beginning LINQ to SQL Using Visual Studio 20...
    - Coding an AjaxPro.NET Based Search Engine fo...
    - Building an AjaxPro.NET Based Search Engine ...

    Click Here




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway