.NET
  Home arrow .NET arrow 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 / 8
    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


    (Page 1 of 5 )

    This is the second article in a series focusing on querying using “LINQ to SQL.” It covers grouping and aggregating data with both SQL and respective LINQ examples in VB.NET and C#.

    If you are a bit new to programming LINQ, please consider reading my previous articles titled “Beginning ‘LINQ to SQL’ using Visual Studio 2008” and “Introducing ‘LINQ to SQL’ designer using Visual Studio 2008.” If you are new to basic LINQ operators, refer to my first article in this series, "Querying LINQ to SQL: Basics."

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



    All of the queries in this article will focus on the above model.

    The entire source code for this article is available in the form of a free downloadable zip file. 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 in execution.

    Working with Aggregation functions (or group functions)

    SQL has several group functions (aggregate functions) to operate on a set of values. MAX, MIN, AVG, COUNT are a few of them. Now, we will try to write LINQ queries based on those functions.

    The following SQL SELECT statement:


    SELECT MAX(Freight) FROM Orders


    Can be written using VB.NET LINQ as follows:


    Dim q = (From p In db.Orders _

    Select p.Freight).Max

    Me.lblResult.Text = q

    And in C# LINQ as follows:


    var q = (from p in db.Orders

    select p.Freight).Max();

    this.lblResult.Text = q.ToString();

    Similarly, all of the following (in VB.NET LINQ) are valid:


    Dim q = (From p In db.Orders _

    Select p.Freight).Sum


    Dim q = (From p In db.Orders _

    Select p.Freight).Min


    Dim q = (From p In db.Orders _

    Select p).Count


    Dim q = (From p In db.Orders _

    Select p.Freight).Average


    The above in C# LINQ would be as follows:


    var q = (from p in db.Orders

    select p.Freight).Sum();


    var q = (from p in db.Orders

    select p.Freight).Min();


    var q = (from p in db.Orders

    select p).Count();


    var q = (from p in db.Orders

    select p.Freight).Average();


    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





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
    Stay green...Green IT