zoukankan      html  css  js  c++  java
  • C#DataTable 使用GroupBy方法的lamda 表达式和Linq语句写法

    https://www.cnblogs.com/johnblogs/p/6006867.html

    DataTable ds = new DataTable();

    //1、lamda 表达式写法(推荐)
    var result = ds.AsEnumerable().GroupBy(s => new{Year = s.Field<int>("Year"), Month = s.Field<int>("Month"), Day = s.Field<int>("Day")});

    //2、Linq写法 最终编译器会把它转化为lamda表达式
    //var result = from s in ds.Tables[0].AsEnumerable()
                 group s by new { Year = s.Field<int>("Year"), Month = s.Field<int>("Month"), Day = s.Field<int>("Day") } into temp

                select temp;

    //DataTable 使用GroupBy方法需要注意result为IGrouping<int,DataRow>类型
    foreach (var thisGroup in result)
            {
                foreach (var row in thisGroup)
                {
                    //遍历当前这组的所有row
                }
            }

     
     
     
     
    //统计不重复的数量,没测试不知是否能用
    dt.DefaultView.ToTable(true, new string[1] { "UserName" }).Rows.Count;
     
     
     
    http://www.xuebuyuan.com/1990057.html
    using (DataTable dt = ds.Tables[0])
            {
                //三个变量分别记录总记录数、不重复的用户数、所有用户的金额总额
                int rowsCount, distinctUserRowsCount, AllUserMoney;
                rowsCount = dt.Rows.Count;
                distinctUserRowsCount = dt.DefaultView.ToTable(true, new string[1] { "UserName" }).Rows.Count;
                AllUserMoney = Convert.ToDecimal(dt.Compute("sum(UserMoney)", ""));
            }
  • 相关阅读:
    提高软件开发内功的推荐阅读列表
    oracle如何使用dblink链接另一个数据库
    代码不可读
    oracle如何使用dblink链接另一个数据库
    程序猿的吐槽三——改进还是不改进
    程序猿的吐槽一
    软件模式
    用户分类
    Matplotlib简介和pyplot的简单使用——subplot
    Linux下Eclipse的PyDev和CDT的安装和配置
  • 原文地址:https://www.cnblogs.com/LuoEast/p/8799161.html
Copyright © 2011-2022 走看看