how to use group by clause in datatable

Posted by Rajass22 under ASP.NET on 10/9/2013 | Points: 10 | Views : 26324 | Status : [Member] | Replies : 4
Hi,

ddd.Table = (DataTable)dscolor.Tables[0];

// ddd.RowFilter = "group by code,price2";
DataRow[] result = ddd.Table.Select("group by price2,code");

i want to use group by clause with datatable in asp.net c#.. but by above methods i got error. please help..




Responses

Posted by: Bandi on: 10/9/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
http://stackoverflow.com/questions/7736831/group-by-in-datatable-column-sum
http://social.msdn.microsoft.com/Forums/en-US/aa631181-f1f5-40d9-b657-f68af7fc3e15/how-to-write-groupby-query-in-datatable-or-dataset?forum=adodotnetdataset

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Rajass22, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 10/9/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Refer
http://maulikdhorajia.blogspot.in/2010/08/apply-group-by-clause-on-datatable-in-c.html

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Rajass22, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Kmandapalli on: 10/9/2013 [Member] Silver | Points: 25

Up
0
Down
Hi,

Try using Linq Query as below...

        DataTable dTable = new DataTable();
dTable.Columns.Add("id",typeof(int));
dTable.Columns.Add("name",typeof(string));
dTable.Rows.Add(1, "hiren");
dTable.Rows.Add(2, "solanki");
dTable.Rows.Add(3, "hiren");
var query = from row in dTable.AsEnumerable()
group row by row.Field<string>("name") into grp
select new
{
Id = grp.Key,
sum = grp.Sum(r=>r.Field<int>("id"))
};
foreach (var grp in query)
{
Response.Write(String.Format("The Sum of '{0}' is {1}", grp.Id, grp.sum));
}


Mark as answer if satisfied....


Regards,
Shree M.

Kavya Shree Mandapalli

Rajass22, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Samirbhogayta on: 10/15/2013 [Member] Starter | Points: 25

Up
0
Down
You can use the linq extensions in the System.Data.DataSetExtensions assembly:

DataTable t = //
var groups = t.AsEnumerable()
.GroupBy(r => r.Field<T>("columnName"))

SAMIR
Sr. Software Engineer

Rajass22, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response