.NET
  Home arrow .NET arrow Page 5 - 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 - Filtering rows with conditions and implementing SQL IN operator using LINQ


    (Page 5 of 6 )

    “WHERE” is one of the most important operators in LINQ and is used to filter rows. Let us imagine that we have the following SELECT query:


    SELECT OrderID, OrderDate, Freight

    FROM Orders

    WHERE Freight > 500 AND Freight < 800


    The relevant LINQ (in VB) would be as follows:


    Dim q = From p In db.Orders _

    Where p.Freight > 500 And p.Freight < 800 _

    Select p.OrderID, p.OrderDate, p.Freight


    The same thing in C# would be as follows:


    var q = from p in db.Orders

    where p.Freight > 500 && p.Freight < 800

    orderby p.Freight descending

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


    The WHERE operator in a SELECT statement is equipped with several SQL operators, such as IN, LIKE, IS NULL, etc. LINQ does not directly support a few of those operators. Instead, we will have to trick LINQ using the .NET Framework. Let us see how.

    The following SELECT query...


    SELECT OrderID, OrderDate, Freight

    FROM Orders

    WHERE OrderID IN (10257, 10300, 10292)


    can be written in LINQ (in VB) as follows:


    Dim arOrderIDs() As Integer = {10257, 10300, 10292}

    Dim q = From p In db.Orders _

    Where arOrderIDs.Contains(p.OrderID) _

    Select p.OrderID, p.OrderDate, p.Freight


    The NOT can be implemented as follows:


    Dim q = From p In db.Orders _

    Where Not (arOrderIDs.Contains(p.OrderID)) _

    Select p.OrderID, p.OrderDate, p.Freight


    In C# (including the NOT operator), the same thing looks like the following:


    int[] arOrderIDs = new int[] { 10257, 10300, 10292 };

    var q = from p in db.Orders

    where !(arOrderIDs.Contains(p.OrderID))

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

    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 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek