.NET
  Home arrow .NET arrow Page 2 - 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  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
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 / 10
    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


    Querying LINQ to SQL: Basics - Column aliasing and expression based columns using LINQ


    (Page 2 of 6 )

    Let us start with the following SELECT Query:

    SELECT RegionID, RegionName = RegionDescription FROM Region

    The following is the code that uses LINQ to represent the above (in VB):


    Protected Sub btnColumnAlias_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnColumnAlias.Click

    Dim db As New NorthwindDataContext

    Dim s As New IO.StringWriter

    db.Log = s


    Dim q = From p In db.Regions _

    Select p.RegionID, RegionName = p.RegionDescription

    Me.GridView1.DataSource = q

    Me.GridView1.DataBind()


    Me.lblGenSQL.Text = s.ToString


    End Sub


    Translating into C#, we have:


    protected void btnColumnAlias_Click(object sender, EventArgs e)

    {

    NorthwindDataContext db = new NorthwindDataContext();

    StringWriter s = new StringWriter();

    db.Log = s;


    var q = from p in db.Regions

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

    this.GridView1.DataSource = q;

    this.GridView1.DataBind();


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

    }


    We can also represent columns with .NET expressions as follows (in VB):


    Dim q = From p In db.Orders _

    Select p.OrderID, p.OrderDate, NoOfDays = Date.Today.Subtract(p.OrderDate).Days


    We can also use the “LET” keyword for better representation as follows:


    Dim q = From p In db.Orders _

    Let u = Date.Today.Subtract(p.OrderDate).Days _

    Select p.OrderID, p.OrderDate, NoOfDays = u


    And we can use any number of “LET”s as follows:


    Dim q = From p In db.Orders _

    Let d = Date.Today.Subtract(p.OrderDate).Days _

    Let y = Year(p.OrderDate) _

    Let m = Month(p.OrderDate) _

    Select p.OrderID, p.OrderDate, d, m, y


    In C#,


    var q = from p in db.Orders

    let u = System.DateTime.Today.Subtract(p.OrderDate.Value).Days

    select new { p.OrderID, p.OrderDate, NoOfDays = u };

    More .NET Articles
    More By Jagadish Chaterjee


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

    .NET ARTICLES

    - Building Applications with Windows Workflow ...
    - Building the Data and Business Layers Using ...
    - The Transformed XML Explorer in MFC
    - List Control and Property Grid with the MFC ...
    - Font, Shell and Masked Edit Controls for MFC
    - Color, Link and Image Editor Controls for M...
    - New Controls for MFC
    - The Windows Ribbon Framework
    - Markup Language for the Ribbon Framework
    - Visually Upgrade Your MFC Project
    - New Features for the Statusbar in MFC
    - Working with the Statusbar in MFC
    - Iron Speed Design v60 Review
    - Binary and XML Serialization
    - Using CrystalReportViewer to Display Crystal...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek