使用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);
});
}
衍生使用
public class Student
{
public string name {get;set;}
public string sex {get;set;}
public decimal score {get;set;}
}
List<Student> query = from t in dt.AsEnumerable()
group t by new { t1 = t.Field<string>("name"), t2 = t.Field<string>("sex") } into m
select new
Student
{
name = m.Key.t1,
sex = m.Key.t2,
score = m.Sum(n => n.Field<decimal>("score"))
};