当前位置: 首页 > article >正文

c# List集合操作帮助类

 public class ListHelper
    {
        #region 赋值转换

        /// <summary>
        /// A实体转换成B实体
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="K"></typeparam>
        /// <param name="source">原始数据对象</param>
        /// <param name="target">目标对象</param>
        /// <returns></returns>
        public static K EntityToEntity<T, K>(T source, K target) where T : class, new() where K : class
        {
            if (source != null && target != null)
            {
                PropertyInfo[] propertys1 = source.GetType().GetProperties();// 获得此模型的公共属性
                PropertyInfo[] propertys = target.GetType().GetProperties();
                foreach (PropertyInfo pi in propertys)
                {
                    if (pi.CanWrite && propertys1.Where(ex => ex.Name == pi.Name).Count() > 0)
                    {
                        object value = GetProperty(source, pi.Name);
                        pi.SetValue(target, value, null);
                    }
                }
            }
            return target;
        }

        /// <summary>
        /// A集合转换成B集合
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="K"></typeparam>
        /// <param name="source">源数据对象</param>
        /// <param name="target">赋值目标对象</param>
        public static List<K> ListToList<T, K>(List<T> source, List<K> target) where T : class, new() where K : class, new()
        {
            if (source != null && target != null)
            {
                foreach (var item in source)
                {
                    var obj = new K();
                    EntityToEntity(item, obj);
                    target.Add(obj);
                }
                return target;
            }
            return null;
        }
        #endregion

        #region 属性设置 取值 

        /// <summary>
        /// 实体获取属性值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="source"></param>
        /// <param name="Name"></param>
        /// <returns></returns>
        public static object GetProperty<T>(T source, string Name) where T : class, new()
        {
            PropertyInfo[] propertys = source.GetType().GetProperties();// 获得此模型的公共属性
            PropertyInfo pi = propertys.Where(ex => ex.Name == Name).FirstOrDefault();
            if (pi != null)
            {
                return pi.GetValue(source, null);
            }
            return null;
        }

        /// <summary>
        /// 实体设置赋值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="source"></param>
        /// <param name="Name"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool SetProperty<T>(T source, string Name, object value) where T : class, new()
        {
            try
            {
                PropertyInfo[] propertys = source.GetType().GetProperties();// 获得此模型的公共属性
                PropertyInfo pi = propertys.Where(ex => ex.Name == Name).FirstOrDefault();
                if (pi != null)
                {
                    pi.SetValue(source, value, null);
                }
                return true;
            }
            catch
            {
                return false;
            }
        }
        #endregion

        #region List<T>集合转DataTable

        /// <summary>
        /// List集合转DataTable
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="list"></param>
        /// <returns></returns>
        public static DataTable ListToDataTable<T>(List<T> list) where T : class, new()
        {
            var Dt = new DataTable(typeof(T).Name);
            PropertyInfo[] propertyInfo = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);

            foreach (PropertyInfo prop in propertyInfo)
            {
                Type t = GetCoreType(prop.PropertyType);
                Dt.Columns.Add(prop.Name, t);
            }

            foreach (T item in list)
            {
                var values = new object[propertyInfo.Length];
                for (int i = 0; i < propertyInfo.Length; i++)
                {
                    values[i] = propertyInfo[i].GetValue(item, null);
                }
                Dt.Rows.Add(values);
            }
            return Dt;
        }

        /// <summary>
        /// 如果类型为空否则返回类型返回基本类型
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        private static Type GetCoreType(Type t)
        {
            var flag = !t.IsValueType || (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable<>));//判断属性是否为空
            if (t != null && flag)
            {
                if (t.IsValueType)//值类型
                {
                    return Nullable.GetUnderlyingType(t);
                }
            }
            return t;
        }

        #endregion 

        #region 集合值对比

        /// <summary>
        /// 集合差异比较器,比较两个实体集合值是否一样,
        /// true 存在差异 false 不存在差异
        /// </summary>
        /// <param name="source">源版本实体集合</param>
        /// <param name="current">当前版本实体集合</param>
        /// <returns>true 存在差异 false 不存在差异</returns>
        public static bool ModelComparison<T1, T2>(List<T1> source, List<T2> current) where T1 : class where T2 : class
        {
            if (source.Count != current.Count) { return true; }
            for (int i = 0; i < source.Count; i++)
            {
                bool flag = ModelComparison(source[i], current[i]);
                if (flag) { return flag; }
            }
            return false;
        }

        /// <summary>
        /// 实体差异比较器,true 存在差异 false 不存在差异
        /// </summary>
        /// <param name="source">源版本实体</param>
        /// <param name="current">当前版本实体</param>
        /// <returns>true 存在差异 false 不存在差异</returns>
        public static bool ModelComparison<T1, T2>(T1 source, T2 current) where T1 : class where T2 : class
        {
            Type t1 = source.GetType();
            Type t2 = current.GetType();
            PropertyInfo[] property1 = t1.GetProperties();
            //排除主键和基础字段 
            foreach (PropertyInfo p in property1)
            {
                string name = p.Name;
                string value1 = p.GetValue(source, null)?.ToString();
                string value2 = t2.GetProperty(name)?.GetValue(current, null)?.ToString();
                if (value1 != value2)
                {
                    return true;
                }
            }
            return false;
        }

        #endregion
    }


http://www.kler.cn/a/230857.html

相关文章:

  • 杭州融资融券利率一般最低是4.5%,两融有哪些核心注意事项?
  • 【SQL高频基础题】619.只出现一次的最大数字
  • java实战:Spring事务控制之事务回滚入门
  • 在flutter中集成Excel导入和导出
  • YOLO部署实战(2):使用OpenCV优化视频转图片流程并设置帧数
  • binlog解析补录数据
  • Redis.conf 配置文件解读
  • C语言-二分查找
  • 第6节、T型加减速转动【51单片机+L298N步进电机系列教程】
  • 代码随想录 Leetcode46. 全排列
  • Web html和css
  • Docker安装PostgreSQL
  • 编程笔记 html5cssjs 073 JavaScript Object数据类型
  • 第5课 使用FFmpeg将rtmp流再转推到rtmp服务器
  • 仰暮计划|“​爷爷说这些话的时候眼睛都红着,他那变形的脊柱和瘸拐的双腿都证明他曾为这个家付出了血汗拼尽了全力”
  • 机器学习-线性回归法
  • Verilog刷题笔记22
  • C# CAD交互界面-自定义窗体(三)
  • 引入BertTokenizer出现OSError: Can‘t load tokenizer for ‘bert-base-uncased‘.
  • Layui 表格组件 头部工具栏 筛选列 加入全选和全不选的功能