Idea List  
Group By Clause Sample
Rate this content:                         
Dear Reader,

With Theme database, we can create Data Context object using LinqToSqlClasses.

 

After that you will have a datacontext for the tables. I have creaeted a LinqtoSqlClass as MyTheme with name ThemeDataContent. This is a

 

sample to create a normal group by using ThemeID and return ThemeID alone

ThemeDataContext objDC = new ThemeDataContext();
var g = from r in objDC.ThemeDetails 
             group r by new { r.ThemeID } into gr
             select new { ThemeID = gr.Key.ThemeID };

For a group with check in the no of records.

 

ThemeDataContext objDC = new ThemeDataContext();
var g = from r in objDC.ThemeDetails
             group r by new { r.ThemeID } into gr
             where gr.Count() > 1
             select new { ThemeID = gr.Key.ThemeID, Count = gr.Count() };

 

For a group with Max Value

 

ThemeDataContext objDC = new ThemeDataContext();
var g = from r in objDC.ThemeDetails
             group r by new { r.ThemeID } into gr
             where gr.Count() > 1
             select new { ThemeID = gr.Key.ThemeID, LastUpdated = gr.Max(p => p.DatedOn), Count = gr.Count() };

 

For a group with Min Value

 

ThemeDataContext objDC = new ThemeDataContext();
var g = from r in objDC.ThemeDetails
             group r by new { r.ThemeID } into gr
             where gr.Count() > 1
              select new { ThemeID = gr.Key.ThemeID, LastUpdated = gr.Max(p => p.DatedOn), CreatedOn = gr.Min(p => p.DatedOn), Count = gr.Count() };

Idea List  
   © 2009 Development Next. All Rights Reserved. | Terms of Use | Trademarks