C# 操作DataTable

            DataTable dt = GetNumbersTable();

            var students = dt.AsEnumerable();

            List<Student> list = students.Select
                (
                    x=> new Student
                    {
                        ID = x.Field<int>("ID"),
                        Name = x.Field<string>("Name")
                    }
                ).ToList();

            foreach (var item in list)
            {
                Console.WriteLine(item.ID + "\t" + item.Name);
            }
        

 

 

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);
    });
} 

 

参考:https://www.cnblogs.com/zhaotianff/p/9248157.html

https://www.cnblogs.com/ChineseMoonGod/p/6899113.html

posted @ 2019-11-18 09:46  wx_h13813744  阅读(107)  评论(0编辑  收藏  举报