.NET
  Home arrow .NET arrow Page 5 - 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  
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

Grouping and Aggregating When Querying LINQ to SQL
By: Jagadish Chaterjee
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 13
    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 - Aggregations in LINQ (like GROUP BY..HAVING of SQL in LINQ)


    (Page 5 of 5 )


    Now, let us consider the following SQL SELECT query:


    SELECT CustomerID, SUM(Freight)

    FROM Orders

    GROUP BY CustomerID

    HAVING SUM(Freight) > 200


    The above does the aggregation first and then filters the output, applying the HAVING condition. The same can be achieved in VB.NET LINQ as follows:


    Dim q = From p In db.Orders _

    Group p By p.CustomerID _

    Into FreightSum = Sum(p.Freight) _

    Where FreightSum > 200 _

    Select _

    CustomerID, _

    FreightSum


    And in C# LINQ as follows:


    var q = from p in db.Orders

    group p.Freight by p.CustomerID

    into g

    where g.Sum() > 200

    select new { CustomerID = g.Key, FreightSum = g.Sum() };


    Further flavors for the same in VB.NET LINQ are as follows:


    Dim q = From p In db.Orders _

    Group p By p.CustomerID _

    Into Group _

    Where Group.Sum(Function(p) p.Freight) > 200 _

    Select _

    CustomerID, _

    FreightSum = Group.Sum(Function(p) p.Freight)


    Dim q = From p In db.Orders _

    Group By p.CustomerID _

    Into FreightSum = Sum(p.Freight) _

    Where FreightSum > 200


    Dim q = From p In db.Orders _

    Group FreightValue = p.Freight By p.CustomerID _

    Into FreightSum = Sum(FreightValue) _

    Where FreightSum > 200

    In this article, I gave many examples in VB.NET than on C#. Several of them can be written in C# syntax also. If you have any doubts, please post in the discussion area.

    We will go further in-depth in my upcoming articles. I hope you enjoyed the article and any suggestions, bugs, errors, enhancements etc. are highly appreciated at http://jagchat.spaces.live.com


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · Hai,This is my second article on LINQ focusing and "Grouping and...
       · Hi, I'm working linq to sql into a project that is still in dev, and I've not seen...
     

    .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 3 Hosted by Hostway
    Stay green...Green IT