zoukankan      html  css  js  c++  java
  • DataTable Linq Example

        DataTable CreateTable()
        {
            DataTable dtable = new DataTable();
            DataColumn dc;
            //MId CId FId PId  
            dc = new DataColumn("MId", Type.GetType("System.Int32"));
            dtable.Columns.Add(dc);
            dc = new DataColumn("CId", Type.GetType("System.Int32"));
            dtable.Columns.Add(dc);
            dc = new DataColumn("FId", Type.GetType("System.Int32"));
            dtable.Columns.Add(dc);
            dc = new DataColumn("PId", Type.GetType("System.String"));
            dtable.Columns.Add(dc);
            return dtable;
        }
    
        void LinqDataTable(HttpResponse Response)
        {
            DataTable dtable  = CreateTable();
            for (int i = 0; i < 30; i++)
            {
                DataRow Dr;
                Dr = dtable.NewRow();
                Dr["MId"] = i;
                Dr["CId"] = i + 1;
                Dr["FId"] = i + 2;
                Dr["PId"] = i%2 ==0? "A":"B";
                dtable.Rows.Add(Dr);
            }
            var query = (from t in dtable.AsEnumerable()
                         //where t.Field<string>("PId") == "A"          
                         orderby t.Field<int>("MId")
                         group t by new
                         {
                             Mid = t.Field<Int32>("MId"),
                             PId = t.Field<string>("PId")
                         } into g
                         //where g.Key.PId == "B"
                         select new
                         {
                             MId = g.Key.Mid,
                             PId = g.Key.PId
                         });
    
            var AGroup = query.Where((a => a.PId == "A"));
            var BGroup = query.Where((a => a.PId == "B"));
            var sum = query.Sum((a => a.PId));
            foreach (var v in AGroup)
            {
                Response.Write("</br>" + v.PId + "	" + v.MId);
            }
            foreach (var v in BGroup)
            {
                Response.Write("</br>" + v.PId + "	" + v.MId);
            }
            Response.End();
        }
    
  • 相关阅读:
    MyEclipse中代码提醒功能
    oracle12c创建用户等问题
    java中的构造块、静态块等说明
    jquery中的get和post、ajax有关返回值的问题描述
    最大半连通子图 BZOJ 1093
    最小生成树计数 BZOJ 1016
    水平可见直线 BZOJ 1007
    分金币 BZOJ 3293
    游走 BZOJ 3143
    糖果 BZOJ 2330
  • 原文地址:https://www.cnblogs.com/fornet/p/5712056.html
Copyright © 2011-2022 走看看