Solved

asp.net datatable group by

Posted on 2010-11-23
6
703 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
[X]
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
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

The most up-to-date version of this article is on my Blog https://iconoun.com/blog/
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

738 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