DataTable 分组

 cmdText = string.Format(@"
             SELECT ID,SENSOR_ID,SENSOR_LOCATION_DESCRIPTION,SAFETY_FACTOR_TYPE_ID,SAFETY_FACTOR_TYPE_NAME
             ,TEMPLATEID,CreateDate,CretaUserName,CretaUserID
             FROM T_DATA_RPTSTRFACTORSENSOR where TEMPLATEID ={0}
            ", templateId);
            DataTable sensorfactordata = SqlHelper.ExecuteDataSetText(cmdText, null).Tables[0];

//进行分组
 var query = from p in sensorfactordata.AsEnumerable()
                        group p by new { factorid = p.Field<int>("SAFETY_FACTOR_TYPE_ID"), factorname = p.Field<string>("SAFETY_FACTOR_TYPE_NAME") } into m
                        select new
                        {
                            factorid = m.Key.factorid,
                            factorname = m.Key.factorname
                        };

 List<ReturnAnalysis> anylist = new List<ReturnAnalysis>();
//分组进行循环
            foreach (var item in query.AsEnumerable())
            {
                ReturnAnalysis anyinfo = new ReturnAnalysis();
                anyinfo.SAFETY_FACTOR_TYPE_ID = item.factorid;
                anyinfo.SAFETY_FACTOR_TYPE_NAME = item.factorname;
//分组后的字段到原始数据进行查询
                var GROUPData = sensorfactordata.Select("SAFETY_FACTOR_TYPE_ID ='" + item.factorid + "'").ToList();
                List<SensrData> sensrlist = new List<SensrData>();
                for (int i = 0; i < GROUPData.Count; i++)
                {
                    SensrData sensrinfo = new SensrData();
                    sensrinfo.SENSOR_ID = int.Parse(GROUPData[i]["SENSOR_ID"].ToString());
                    sensrinfo.SENSOR_LOCATION_DESCRIPTION = GROUPData[i]["SENSOR_LOCATION_DESCRIPTION"].ToString();
                    sensrlist.Add(sensrinfo);
                }
                anylist.Add(anyinfo);

                }

 

posted @ 2022-03-16 16:12  小小小菜鸟1  阅读(321)  评论(0编辑  收藏  举报