C # uses DataTable for group by data statistics

Source: Internet
Author: User

1, with two-layer cycle calculation, the precondition is that the data has been grouped by the column of the order.

DataTable dt = new DataTable ();
Dt. Columns.addrange (new datacolumn[] {new DataColumn ("name", typeof (String)),
New DataColumn ("Sex", typeof (String)),
New DataColumn ("Score", typeof (int))});
Dt. Rows.Add (new object[] {"Zhang San", "Male", 1});
Dt. Rows.Add (new object[] {"Zhang San", "Male", 4});
Dt. Rows.Add (new object[] {"John Doe", "Male", 100});
Dt. Rows.Add (new object[] {"John Doe", "female", 90});
Dt. Rows.Add (new object[] {"Harry", "Female", 77});
DataTable dtresult = dt. Clone ();
for (int i = 0; i < dt. Rows.Count; )
{
DataRow dr = Dtresult.newrow ();
String name = dt. rows[i]["Name"]. ToString ();
string sex = dt. rows[i]["Sex"]. ToString ();
dr["name"] = name;
dr["sex"] = sex;
int score = 0;
The inner layer also loops the same table and jumps out of the inner loop when different name is encountered
for (; i < dt. Rows.Count; )
{
if (name = = dt. rows[i]["Name"]. ToString () &&name = = dt. rows[i]["Sex"]. ToString ())
{
Score + = Convert.ToInt32 (dt. rows[i]["Score"]);
dr["score"] = score;
i++;
}
Else
{
Break
}
}
DTRESULT.ROWS.ADD (DR);

}

The data in Dtresult is the final result.

2, using the Compute method of the DataTable, the data in the DataTable is not ordered in advance.

DataTable dt = new DataTable ();
Dt. Columns.addrange (new datacolumn[] {new DataColumn ("name", typeof (String)),
New DataColumn ("Sex", typeof (String)),
New DataColumn ("Score", typeof (int))});
Dt. Rows.Add (new object[] {"Zhang San", "Male", 1});
Dt. Rows.Add (new object[] {"Zhang San", "Male", 4});
Dt. Rows.Add (new object[] {"John Doe", "Male", 100});
Dt. Rows.Add (new object[] {"John Doe", "female", 90});
Dt. Rows.Add (new object[] {"Harry", "Female", 77});
DataTable dtresult = dt. Clone ();
DataTable dtname = dt. Defaultview.totable (True, "name", "Sex");
for (int i = 0; i < DtName.Rows.Count; i++)
{
datarow[] rows = dt. Select ("Name=" + dtname.rows[i][0] + "' and sex= '" + dtname.rows[i][1] + "'");
Temp is used to store filtered data.
DataTable temp = Dtresult.clone ();
foreach (DataRow row in rows)
{
Temp. Rows.Add (row. ItemArray);
}

DataRow dr = Dtresult.newrow ();
Dr[0] = dtname.rows[i][0]. ToString ();
DR[1] = Temp.compute ("sum (score)", "");
DTRESULT.ROWS.ADD (DR);

}

3. Using LINQ to DataTable Group by

var query = from T in dt. AsEnumerable ()
Group T by new {T1 = t.field<string> (' name '), t2 = t.field<string> ("Sex")} into M
Select New
{
Name = M.key.t1,
Sex = M.key.t2,
Score = m.sum (n = n.field<decimal> ("score"))
};
if (query. ToList (). Count > 0)
{
Query. ToList (). ForEach (q =
{
Console.WriteLine (Q.name + "," + Q.sex + "," + Q.score);
});
}

C # uses DataTable for group by data statistics

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.