CODEDIGEST
Home » Articles
Search
 

Technologies
 

Sponsored links
 

CodeDigest Navigation
 

Technology News
No News Feeds available at this time.
 

Community News
No News Feeds available at this time.
 
Using GroupBy in LINQDatasource Control for Grouping Data

By Satheesh Babu
Posted On Dec 04,2009
Article Rating:
Be first to rate
this article.
No of Comments: 0
Category: ASP.Net
Print this article.

Using GroupBy in LINQDataSource Control for Grouping Data

 

My previous article Using Stored Procedure with LINQDataSource Control in ASP.Net discussed on using stored procedures when using LINQDataSource control.

Grouping data is one of the most common activities we do with any data driven websites. LINQDataSource control allows us to group data based on one or more columns with the help of GroupBy property and Select property.  Moving forward, we will see how to group the data when using LinqDataSource control.

 

To understand this article, we will create a sample application that displays employee information by grouping the data from Employee and Dept table.

Steps

1.      From the start menu > All Programs > Open Visual Studio 2008.

2.      Create a new Asp.Net website by clicking New > Website.

3.      Use a language of your choice, i have selected C# as the language.

4.      Next, create a new SQL express database with Employee and Dept table.

 

As we all know, in order to work with LINQDataSource control, we need to first design our LINQ to SQL or LINQ Entity classes.

 

Designing the LINQ to SQL classes

I assume you have already created a SqlExpress database in App_Data folder with Employee and Department table.

1.      Open Server Explorer, Expand the database tables.

2.      Select and drag Employee and Dept table into the dbml file(LINQ to SQL designer). Click Save. The LINQ to SQL Objects and the data context class will be created automatically.

Refer the below figure.

 LINQDataSource control with stored procedure

 

Using GroupBy with LINQDataSource Control

To understand grouping data with LINQDataSource, we will fetch the maximum employee age in each department and populate it on the GridView.

 

<asp:GridView ID="GridView1" runat="server"

        AutoGenerateColumns="False" DataSourceID="LinqDataSource1" >

         <Columns>   

         <asp:BoundField DataField="Department" HeaderText="Department"

         ReadOnly="True" SortExpression="DeptID" />

         <asp:BoundField DataField="MaxAge" HeaderText="MaxAge"

         ReadOnly="True"  />

        </Columns>

  </asp:GridView>

     

    <asp:LinqDataSource ID="LinqDataSource1" runat="server"

        ContextTypeName="DataClassesDataContext" TableName="Employees"

        GroupBy="DeptID"

        Select="new(key as Department,Max(Age) as MaxAge)" >         

    </asp:LinqDataSource>

 

This will give the following output.

 

In the above code, if we see the Select property of LINQDataSource i have used a keyword called key. We can use this keyword to fetch the data column specified in GroupBy property. Just specify an alias to the keyword to display it in gridview. The LINQDataSource control is configured to group and fetch the DeptID.

You can change the GroupBy property to below to fetch the Department name instead.

Refer below,

<asp:LinqDataSource ID="LinqDataSource1" runat="server"

        ContextTypeName="DataClassesDataContext" TableName="Employees"

        GroupBy="Dept.Department"

        Select="new (key as Department,Max(Age) as MaxAge)">

    </asp:LinqDataSource>

 

Grouping on Multiple Columns with LINQDataSource Control

We can also group the data on multiple columns using LINQDataSource control’s GroupBy property. For example, to fetch the maximum age in each department on different locations we need to group data on Dept and Location.

 

 <asp:GridView ID="GridView1" runat="server"

        AutoGenerateColumns="False" DataSourceID="LinqDataSource1" >

         <Columns>   

         <asp:BoundField DataField="Department" HeaderText="Department"

         ReadOnly="True" SortExpression="DeptID" />

         <asp:BoundField DataField="MaxAge" HeaderText="MaxAge"

         ReadOnly="True"  />

          <asp:BoundField DataField="City" HeaderText="City"

         ReadOnly="True"  />     

        </Columns>

        </asp:GridView>

<asp:LinqDataSource ID="LinqDataSource1" runat="server"

        ContextTypeName="DataClassesDataContext" TableName="Employees"

        GroupBy="new(Location,Dept.Department)"

        Select="new (key.Location as City,key.Department as Department, Max(Age) as MaxAge)" >

    </asp:LinqDataSource>

 




Fetching the Grouped Rows in LINQDatasource control

In order to fetch the grouped rows, we can use the keyword called “it” in Select property which will return us the collection of individual rows that are grouped. This keyword should be given an alias similar to key keyword.

 

Refer the below code,

<asp:GridView ID="GridView1" runat="server"

        AutoGenerateColumns="False" DataSourceID="LinqDataSource1" >

         <Columns>   

               <asp:BoundField DataField="Department" HeaderText="Department"

                ReadOnly="True" SortExpression="DeptID" />

              <asp:BoundField DataField="MaxAge" HeaderText="MaxAge"

              ReadOnly="True"  />

               <asp:BoundField DataField="City" HeaderText="City"

               ReadOnly="True"  />

              <asp:TemplateField HeaderText="GroupedRows">

               <ItemTemplate>

                     <asp:GridView runat="server" DataSource='<%# Eval("Employees") %>'></asp:GridView>

               </ItemTemplate>

              </asp:TemplateField>

        </Columns>

        </asp:GridView>

 

 <asp:LinqDataSource ID="LinqDataSource1" runat="server"

        ContextTypeName="DataClassesDataContext" TableName="Employees"

        GroupBy="new(Dept.Department,Location)"

        Select="new (key.Location as City,key.Department as Department, it as Employees,Max(Age) as MaxAge)">

    </asp:LinqDataSource>

 

When the above code is executed, we will get an output similar to below figure.

 

Downloads

Download Source 

Conclusion

Like any other datasource control LINQDataSource control also helps us in developing data driven application very easily. By this article, we understood the flexibility of LINQDataSource control to support grouping of data by just doing some configuration settings. Thanks for reading this article.

Happy Coding!!

 

Similar Articles
You can contribute to CodeDiget.Com:
Donate to CodeDigest.com
Article Feedback
Comments