• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 713
  • Last Modified:

asp.net datatable group by

I have a data table in asp.net.  i would like to do a group by and display only unique values of rows.

Product 1, Type 1
Product 1, Type 1
Product 2, Type 1
Product 2, Type 2

I would like to see 3 rows in this case.

Is this possible?
0
Fraser_Admin
Asked:
Fraser_Admin
  • 3
  • 2
1 Solution
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
can you not do the group by on the query level?
 select col1, col2 from yourtable group by col1, col2 

Open in new window


apart: short answer is no.
you can, of course, build some functions like this:
http://weblogs.sqlteam.com/davidm/archive/2004/05/20/1351.aspx

but you might prefer the SQL method ...
0
 
Fraser_AdminAuthor Commented:
can i do it threw linq.  i am begining to look at it, but can't make much sense out of it so far.  i would like to use linq to do the group by then put it back into a datatable.  is this possible?

no i want to do it in my business logic.  i don't want to write a whole new query when all i need is a subset of the existing one.
0
 
Fraser_AdminAuthor Commented:
also where is the sqlops class on the link you provided?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
0
 
Fraser_AdminAuthor Commented:
can advise me on how to do a group by on a data table which has 2 columns.  these examples do not seem to use datatables.  i would like to have a function which accepts a datatable as a parameter and then outputs a datatable.  thanks for your help.
0
 
abhinayp86Commented:
Here is the sample
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DataTable dtTemp = new DataTable();
        dtTemp.Columns.Add("Id", typeof(int));
        dtTemp.Columns.Add("Value", typeof(decimal));
        dtTemp.Rows.Add(123, 4.0M);
        dtTemp.Rows.Add(123, 5.0M);
        dtTemp.Rows.Add(234, 1.0M);
        dtTemp.Rows.Add(345, 2.0M);
        dtTemp.Rows.Add(345, 3.0M);
        
        List<clsSample> c = (from table in dtTemp.AsEnumerable()
                     group table by new { column1 = table["Id"], column2 = table["Value"] }
                         into groupedTable
                         select new clsSample()
                         {

                             i = groupedTable.Key.column1.ToString(),
                             val = groupedTable.Key.column2.ToString()
                         }).ToList();
        GridView1.DataSource = c;
        GridView1.DataBind();
    }
    private class clsSample
    {
        public string i { get; set; }
        public string val { get; set; }
    }
}

Open in new window

0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now