Solved

asp.net datatable group by

Posted on 2010-11-23
6
697 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 142

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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 142

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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

803 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