.NET
  Home arrow .NET arrow Page 4 - 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 - Sorting rows with ORDER BY using LINQ


    (Page 4 of 6 )

    Let us imagine that I have the following SELECT query:


    SELECT OrderID, OrderDate FROM Orders


    And now, we will represent the same thing using LINQ (in VB). This is the code:


    Dim q = From p In db.Orders _

    Select p.OrderID, p.OrderDate _

    Order By OrderDate


    The sorting order defaults to “ascending” when no specific order type is defined. We can define it as “descending” as follows:


    Dim q = From p In db.Orders _

    Select p.OrderID, p.OrderDate _

    Order By OrderDate Descending


    Nothing can stop you from defining it as “ascending” or “descending” in your query. The same can be achieved in C# as follows:


    var q = from p in db.Orders

    orderby p.OrderDate

    select new { p.OrderID, p.OrderDate };


    If multiple columns need to be sorted as they are provided in the following SELECT query,


    SELECT OrderID, OrderDate FROM Orders

    ORDER BY OrderDate DESC, OrderID DESC


    then the relevant LINQ (in VB) would be as follows:


    Dim q = From p In db.Orders _

    Select p.OrderID, p.OrderDate _

    Order By OrderDate Descending , OrderID Descending 


    The same can be achieved in C# as follows:


    var q = from p in db.Orders

    orderby p.OrderDate descending, p.OrderID descending 

    select new { p.OrderID, p.OrderDate };

    More .NET Articles
    More By Jagadish Chaterjee


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

    .NET ARTICLES

    - 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...
    - Creating Summary .Net Crystal Reports
    - More on Commands, Input and the WPF





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