.NET
  Home arrow .NET arrow Page 2 - Grouping and Aggregating When Querying LIN...
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

Grouping and Aggregating When Querying LINQ to SQL
By: Jagadish Chaterjee
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 9
    2008-07-21

    Table of Contents:
  • Grouping and Aggregating When Querying LINQ to SQL
  • Querying with LINQ “COUNT” in different contexts
  • Aggregations in LINQ (like GROUP BY of SQL in LINQ)
  • Aggregations in LINQ (like GROUP BY of SQL in LINQ): continued
  • Aggregations in LINQ (like GROUP BY..HAVING of SQL in 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


    Grouping and Aggregating When Querying LINQ to SQL - Querying with LINQ “COUNT” in different contexts


    (Page 2 of 5 )

    One of the most frequently used functions is COUNT. It comes in different flavors. The following three have different meanings:


    SELECT COUNT(*) FROM Orders


    SELECT COUNT(ShipRegion) FROM Orders


    SELECT COUNT(DISTINCT ShipRegion) FROM Orders



    The first returns the total number of rows available in the Orders table. The second returns the number of non-null values in the “ShipRegion” column of the Orders table. The third returns the number of values in the “ShipRegion” column without counting the repeated and null values.

    From the point of view of LINQ, it always counts objects (and not values) in the collection. So, both of the following would return the same count values (i.e., total number of objects in Orders).


    Dim q = (From p In db.Orders _

    Select p).Count


    Dim q = (From p In db.Orders _

    Select p.ShipRegion).Count

    In both of the above cases, the SQL SELECT which gets generated from the above LINQ would be same (with COUNT(*)). There exists no direct way in LINQ to achieve the above type of SQL SELECT query. But we can do it in an indirect manner.

    I would like to rewrite the following query:


    SELECT COUNT(ShipRegion) FROM Orders


    in the following manner:


    SELECT COUNT(*) FROM Orders WHERE ShipRegion IS NOT NULL


    The same can be represented in LINQ (VB.NET) as follows:


    Dim q = (From p In db.Orders _

    Where p.ShipRegion IsNot Nothing _

    Select p.ShipRegion).Count


    You can observe that I am explicitly filtering nulls in the above LINQ query. The same can be achieved in C# LINQ as follows:


    var q = (from p in db.Orders

    where p.ShipRegion != null 

    select p.ShipRegion).Count();


    And now, the following SQL SELECT:


    SELECT COUNT(DISTINCT ShipRegion) FROM Orders


    can be written in VB.NET LINQ as follows:


    Dim q = (From p In db.Orders _

    Where p.ShipRegion IsNot Nothing _

    Select p.ShipRegion).Distinct.Count


    And of course, in C# LINQ as follows:


    var q = (from p in db.Orders

    where p.ShipRegion != null

    select p.ShipRegion).Distinct().Count();


    Note that during the execution of above LINQ queries, the SQL SELECT which gets generated would be entirely different from what I have posted above. But, finally, the result would be same.

    More .NET Articles
    More By Jagadish Chaterjee


       · Hai,This is my second article on LINQ focusing and "Grouping and...
     

    .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 5 hosted by Hostway
    Stay green...Green IT