用 .SqlSugar ORM 实现行转列
作者:互联网
1、SqlSugar.cs
SqlSugar作为一款.NET老牌ORM 并且也是 新手基数比较多的ORM。
2、SQL实现报表
1. Sql可以很方便的创建临时表并且关联
2.Sql语法事先行转列,列转行等一系列的操作
3、实例
1、正常写法
1 var days = (time.AddMonths(1) - time).Days;//获取1月天数 2 var dayArray = Enumerable.Range(1, days).Select(it=> time.ToString("yyyy-MM-"+it)).ToList();//转成时间数组 3 var queryableLeft = db.Reportable(dayArray).ToQueryable<DateTime>(); 4 var queryableRight = db.Queryable<operateinfo>(); 5 var list = db.Queryable(queryableLeft, queryableRight, JoinType.Left, 6 (x1, x2) => x1.ColumnName.Date==x2.operate_time.Date) 7 .GroupBy((x1, x2) => x1.ColumnName) 8 .Select((x1, x2) => new 9 { 10 count = SqlFunc.AggregateSum(SqlFunc.IIF(x2.id > 0, 1, 0)), 11 day = x1.ColumnName.Day 12 13 }).ToList();
2、实现行转列
1 var days = (time.AddMonths(1) - time).Days;//获取1月天数 2 var dayArray = Enumerable.Range(1, days).Select(it=> time.ToString("yyyy-MM-"+it)).ToList();//转成时间数组 3 var queryableLeft = db.Reportable(dayArray).ToQueryable<DateTime>(); 4 var queryableRight = db.Queryable<operateinfo>(); 5 var list = db.Queryable(queryableLeft, queryableRight, JoinType.Left, 6 (x1, x2) => x1.ColumnName.Date==x2.operate_time.Date) 7 .GroupBy((x1, x2) => x1.ColumnName) 8 .Select((x1, x2) => new 9 { 10 count = SqlFunc.AggregateSum(SqlFunc.IIF(x2.id > 0, 1, 0)), 11 day = x1.ColumnName.Day, 12 name="合计" 13 14 }).ToPivotTable(it => it.day, it => it.name, it => it.Sum(x => x.count));
文章出处:用 .SqlSugar ORM 来实现报表功能 .NET CORE /.NET - 果糖大数据科技 - 博客园 (cnblogs.com)
标签:ColumnName,db,x2,转列,ORM,time,var,x1,SqlSugar 来源: https://www.cnblogs.com/houshanwen/p/14980142.html