(精华)2020年6月26日 C#类库 数据库统计帮助类(扩展方法)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Reflection;
using System.Text;

namespace Core.Util
{
    /// 
    /// 数据库查询帮助类
    /// 
    public static class DbSearchHelper
    {
        /// 
        /// 获取数据库统计数据
        /// 
        /// 数据源
        /// 分组的列
        /// 统计的列
        /// 统计方法名(Max,Min,Average,Count())
        /// 
        public static List<DbStatisData> GetDbStatisData(this IQueryable dataSource, string groupColumn, string statisColumn, string funcName)
        {
            List<DbStatisData> resData = new List<DbStatisData>();
            var q = dataSource.GroupBy(groupColumn, "it")
            .Select($"new (it.Key as Key,{funcName}(it.{statisColumn}) as Value)")
            .CastToList<dynamic>();
            foreach (dynamic aData in q)
            {
                DbStatisData newData = new DbStatisData();
                resData.Add(newData);
                
                newData.Key = aData.Key;
                newData.Value = aData.Value;
            }

            return resData;
        }

        /// 
        /// 获取数据库统计数据
        /// 
        /// 数据源
        /// 分组的列
        /// 查询的配置项
        /// 
        public static List<DynamicModel> GetDbStatisData(this IQueryable dataSource, string groupColumn, SearchEntry[] searchEntris)
        {
            if (searchEntris.Count() == 0)
                throw new Exception("请输入至少一个查询配置项");
            StringBuilder selectStr = new StringBuilder();
            selectStr.Append("new (it.Key as Key");
            int count = searchEntris.Count();
            searchEntris.ForEach((item, index) => {
                string end = "";
                if (index == count - 1)
                    end = ")";
                selectStr.Append($",{item.FuncName}(it.{item.StatisColoum}) as {item.ResultName}{end}");
            });

            List<DynamicModel> resData = new List<DynamicModel>();
            var q = dataSource.GroupBy(groupColumn, "it")
            .Select(selectStr.ToString())
            .CastToList<dynamic>();
            foreach (dynamic aData in q)
            {
                DynamicModel newData = new DynamicModel();
                resData.Add(newData);
                object obj = (object)aData;
                Type type = obj.GetType();
                newData.AddProperty("Key", type.GetProperty("Key").GetValue(obj));
                searchEntris.ForEach(item =>
                {
                    newData.AddProperty(item.ResultName, type.GetProperty(item.ResultName).GetValue(obj));
                });
            }

            return resData;
        }

        /// 
        /// 获取IQueryable
        /// 
        /// 包含获取IQueryable方法的对象
        /// 获取IQueryable的方法名
        /// 实体名
        /// 命名空间
        /// 
        public static IQueryable GetIQueryable(object obj, string funcName, string entityName, string nameSpace)
        {
            Type type = obj.GetType();
            MethodInfo method = type.GetMethod(funcName);
            var entityType = Assembly.Load(nameSpace).GetTypes().Where(x => x.Name.ToLower().Contains(entityName.ToLower())).FirstOrDefault();
            if (entityType.IsNullOrEmpty())
                throw new Exception("请输入有效的实体名!");

            var iQueryable = (IQueryable)method.MakeGenericMethod(entityType).Invoke(obj, null);

            return iQueryable;
        }
    }

    /// 
    /// 统计数据模型
    /// 
    public class DbStatisData
    {
        /// 
        /// 分组查询的列
        /// 
        public string Key { get; set;}

        /// 
        /// 统计后的数值
        /// 
        public double? Value { get; set; }
    }

    /// 
    /// 统计查询配置项
    /// 
    public struct SearchEntry
    {
        /// 
        /// 统计的列
        /// 
        public string StatisColoum { get; set; }

        /// 
        /// 返回数据列名
        /// 
        public string ResultName { get; set; }

        /// 
        /// 统计方法名(Max,Min,Average,Count()等)
        /// 
        public string FuncName { get; set; }
    }
}

你可能感兴趣的:(#,C#类库/扩展方法)