zoukankan      html  css  js  c++  java
  • (转)C#用Linq实现DataTable的Group by数据统计

    本文转载自:http://www.cnblogs.com/sydeveloper/archive/2013/03/29/2988669.html

    1、用两层循环计算,前提条件是数据已经按分组的列排好序的。

    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[] { "张三","",1 });
    dt.Rows.Add(new object[] { "张三","",4 });
    dt.Rows.Add(new object[] { "李四","",100 });
    dt.Rows.Add(new object[] { "李四","",90 });
    dt.Rows.Add(new object[] { "王五","",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;
        //内层也是循环同一个表,当遇到不同的name时跳出内层循环
        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);
    }

    dtResult中的数据即是最终结果。 

    2、 借助DataTable的Compute方法,DataTable中数据不用事先排好序。

    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[] { "张三","",1 });
    dt.Rows.Add(new object[] { "张三","",4 });
    dt.Rows.Add(new object[] { "李四","",100 });
    dt.Rows.Add(new object[] { "李四","",90 });
    dt.Rows.Add(new object[] { "王五","",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用来存储筛选出来的数据
        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、使用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);
        });
    } 
  • 相关阅读:
    设计模式学习笔记——状态模式(state)
    设计模式学习笔记——工厂方法模式(Factory Method)
    设计模式学习笔记——抽象工厂(Abstract Factory)
    设计模式学习笔记——单例模式(Singleton)
    设计模式学习笔记——适配器模式(Adapter)
    设计模式学习笔记——建造者模式(Builder)
    设计模式学习笔记——观察者模式(Observer)
    玩转Node.js(三)
    玩转Node.js(二)
    玩转Node.js(一)
  • 原文地址:https://www.cnblogs.com/wpcnblog/p/4118789.html
Copyright © 2011-2022 走看看