Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

asp.net datatable group by

Posted on 2010-11-23
6
698 Views
Last Modified: 2012-06-27
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
Comment
Question by:Fraser_Admin
  • 3
  • 2
6 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 34197292
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
 

Author Comment

by:Fraser_Admin
ID: 34197448
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
 

Author Comment

by:Fraser_Admin
ID: 34197527
also where is the sqlops class on the link you provided?
0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 34197563
0
 

Author Comment

by:Fraser_Admin
ID: 34197618
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
 
LVL 9

Accepted Solution

by:
abhinayp86 earned 500 total points
ID: 34198369
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Got endorsements from your clients?  Great!  There is almost nothing better than word-of-mouth advertising.  But how can you do that on the internet?  Sure you can make a page for endorsement quotations and list them all, but who is …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question