Share via


how can i set GROUP BY in datatable select?

Question

Wednesday, October 12, 2011 11:24 AM

hi

i have a filled data table,how can i set GROUP BY  by in one column and fill a datarow collection??

like this but it makes error

myDatatable.Select("GROUP BY CatName"")

Thanks Motevallizadeh

All replies (3)

Wednesday, October 12, 2011 11:46 AM âś…Answered | 1 vote

Hi Motevallizadeh,

try this:

var groupbyfilter= from d in  objClsDB.MyDataSet.Tables["Category"].AsEnumerable() group d by d["CatName"];

 

Regards, http://shwetamannjain.blogspot.com


Wednesday, October 12, 2011 11:40 AM

 Implement a DataSet GROUP BY Helper Class in Visual C# .NET

http://support.microsoft.com/kb/326145

Other link

http://stackoverflow.com/questions/4471499/linq-datatable-group-by-question-in-c

http://www.codeproject.com/KB/linq/SampleGroupbyLINQ.aspx

Regards,

Narendran Ponpandiyan


Wednesday, October 12, 2011 12:37 PM

try using a DataTable Expression.
here is the sample code:

using System.Data;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            // create data
            var dt = new DataTable();
            dt.Columns.Add("Id", typeof(int));
            dt.Columns.Add("Key", typeof(string));
            dt.Columns.Add("Value", typeof(string));
            for (int i = 0; i < 10; i++)
                dt.Rows.Add(i, "key" + i, "value" + (i % 2));

            // set a filter
            var dv = new DataView(dt);
            // refer syntax http://msdn.microsoft.com/en-us/library/system.data.datacolumn.expression.aspx
            dv.RowFilter = "Value LIKE 'value0'";
            
            var dg = new DataGrid();
            dg.Dock = DockStyle.Fill;
            dg.DataSource = dv;
            dg.Parent = this;
        }
    }
}