Solved

asp.net datatable group by

Posted on 2010-11-23
6
695 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]
Comment Utility
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
Comment Utility
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
Comment Utility
also where is the sqlops class on the link you provided?
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
0
 

Author Comment

by:Fraser_Admin
Comment Utility
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
Comment Utility
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

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 A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

763 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now