?
Solved

asp.net datatable group by

Posted on 2010-11-23
6
Medium Priority
?
711 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 2000 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

801 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