赞
踩
// C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.8\System.Data.DataSetExtensions.dll
我项目引用的是.NETFramework\v4.8下的一个扩展.dll
- public List<CaseStatusModel> GetTECaseStatus()
- {
- using (var db = new DBCommon())
- {
- var result = new List<CaseStatusModel>();
- string sql = @"with
- t as(
- select count(ID) CaseCount,IsActive,[Period] from TE_T_TESummaryCase where 1=1 group by IsActive ,[Period]
- )
- select top 12 t.[Period]
- ,sum(case t.IsActive when 1 then t.CaseCount else 0 end) as PendingCase
- ,sum(case t.IsActive when 0 then t.CaseCount else 0 end) as ClosedCase
- from t
- group by t.[Period]
- order by t.[Period] desc";
- DataTable dt = db.ExecuteQuery(sql.ToString());
- if (dt != null && dt.Rows.Count > 0)
- {
- result = dt.AsEnumerable().Select(t => new CaseStatusModel()
- {
- //可空选项
- Month = t.Field<DateTime?>("Period")?.ToString("yyyy-MM"),
- PendingCase = t.Field<int>("PendingCase"),
- ClosedCase = t.Field<int>("ClosedCase")
- }).ToList();
- }
- return result;
- }
- }
原表长这样
数据库查询结果长这样
List返回长这样
以前项目没有数据对象映射方法的时候,for循环遍历DataTable感觉没有这种精简吧,特此记录
仅供学习参考,如有侵权联系我删除
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。