DataTable dataTable = new DataTable();
dataTable.Columns.Add("username", typeof(string)); dataTable.Rows.Add("zgj");
dataTable.Rows.Add("zgj");
dataTable.Rows.Add("zgj1");
dataTable.Rows.Add("zgj1");
dataTable.Rows.Add("zgj1"); var query = from t in dataTable.AsEnumerable()
group t by new { t1 = t.Field<string>("username") } into m
select new
{
username = m.Key.t1,
usernamecount = m.Count()
}; foreach (var q in query)
{
Console.WriteLine(q.username + ":" + q.usernamecount);
}

输出结果

zgj:2
zgj1:3

带where 语句写法

var query = from t in dataTable.AsEnumerable()
where t["username"].ToString()=="zgj"
group t by new { t1 = t.Field<string>("username") } into m
select new
{
username = m.Key.t1,
usernamecount = m.Count()
};
05-28 13:30