|
|
|
สอบถามเรื่องการทำ Group By ข้อมูลที่ซ้ำกันในตัว DataTable |
|
|
|
|
|
|
|
Code (C#)
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("ID"));
DataRow dr = dt.NewRow();
dr["ID"] = "x001";
dt.Rows.Add(dr);
DataRow dr1 = dt.NewRow();
dr1["ID"] = "x002";
dt.Rows.Add(dr1);
DataRow dr2 = dt.NewRow();
dr2["ID"] = "x001";
dt.Rows.Add(dr2);
DataRow dr3 = dt.NewRow();
dr3["ID"] = "x003";
dt.Rows.Add(dr3);
DataRow dr4 = dt.NewRow();
dr4["ID"] = "x004";
dt.Rows.Add(dr4);
DataRow dr5 = dt.NewRow();
dr5["ID"] = "x002";
dt.Rows.Add(dr5);
var grouped = from table in dt.AsEnumerable()
group table by new { IDcol = table["ID"] } into groupby
select new
{
columnValues = groupby.Key
};
string str = string.Empty;
foreach (var key in grouped)
{
str += key.columnValues.IDcol
str += Environment.NewLine;
}
MessageBox.Show(str);
|
ประวัติการแก้ไข 2013-07-13 18:25:00
|
|
|
|
Date :
2013-07-13 17:39:06 |
By :
01000010 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Code (C#)
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("ID"));
DataRow dr = dt.NewRow();
dr["ID"] = "x001";
dt.Rows.Add(dr);
DataRow dr1 = dt.NewRow();
dr1["ID"] = "x002";
dt.Rows.Add(dr1);
DataRow dr2 = dt.NewRow();
dr2["ID"] = "x001";
dt.Rows.Add(dr2);
DataRow dr3 = dt.NewRow();
dr3["ID"] = "x003";
dt.Rows.Add(dr3);
DataRow dr4 = dt.NewRow();
dr4["ID"] = "x004";
dt.Rows.Add(dr4);
DataRow dr5 = dt.NewRow();
dr5["ID"] = "x002";
dt.Rows.Add(dr5);
string str = string.Empty;
var grouped = dt.AsEnumerable().GroupBy(r => r.Field<string>("ID")).Select(g => new { ID = g.Key , Total = g.Count()});
foreach (var key in grouped)
{
str += key.ID;
str += " " + key.Total;
str += Environment.NewLine;
}
MessageBox.Show(str);
อย่างนี้ก็ได้ครับ
|
|
|
|
|
Date :
2013-07-13 18:18:49 |
By :
01000010 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
จัดไปครับ
|
|
|
|
|
Date :
2013-07-13 20:44:31 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ขอบคุณนะครับ
|
|
|
|
|
Date :
2013-07-15 07:13:52 |
By :
Agility |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 05
|