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

ListExtension 扩展方法增加 转DataTable()方法

方法

        1.判断集合是否为Null

        2.判断集合是否不为Null

        3.判断集合是否有值

        4.拼接成字符串

        5.根据一个表达式去除重复

        6.转DataTable 泛型对象转换方法

/// <summary>
/// 集合扩展方法
/// </summary>
public static class ListExtension
{/// <summary>/// 判断集合是否为Null/// </summary>/// <typeparam name="T"></typeparam>/// <param name="list"></param>/// <returns></returns>public static bool IsNull<T>(this IEnumerable<T> list){return list == null;}/// <summary>/// 判断集合是否不为Null/// </summary>/// <typeparam name="T"></typeparam>/// <param name="list"></param>/// <returns></returns>public static bool IsNotNull<T>(this IEnumerable<T> list){return list != null;}/// <summary>/// 判断集合是否有值/// </summary>/// <typeparam name="T"></typeparam>/// <param name="list"></param>/// <returns></returns>public static bool IsHaveVal<T>(this IEnumerable<T> list){return list != null && list.Count() > 0;}/// <summary>/// 拼接成字符串/// </summary>/// <param name="list"></param>/// <param name="split">分隔筏</param>/// <returns></returns>public static string JoinToString(this IEnumerable<int> list, string split){if (list != null)return string.Join(split, list);return string.Empty;}/// <summary>/// 拼接成字符串/// </summary>/// <param name="list"></param>/// <param name="split">分隔筏</param>/// <returns></returns>public static string JoinToString(this IEnumerable<string> list, string split){if (list != null)return string.Format("'{0}'",string.Join("'" + split + "'", list));return string.Empty;}/// <summary>/// 根据一个表达式去除重复/// </summary>/// <typeparam name="TSource">数据源类型</typeparam>/// <typeparam name="TKey">过滤属性类型</typeparam>/// <param name="source">数据源集合</param>/// <param name="keySelector">过滤表达式</param>/// <returns></returns>public static IEnumerable<TSource> DistinctBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector){HashSet<TKey> seenKeys = new HashSet<TKey>();foreach (TSource element in source){if (seenKeys.Add(keySelector(element))){yield return element;}}}/// <summary>/// 转DataTable 泛型对象转换方法/// </summary>/// <typeparam name="T"></typeparam>/// <param name="items"></param>/// <returns></returns>public static DataTable ToDataTable<T>(this List<T> items){DataTable dt = new DataTable(typeof(T).Name);// 创建列var props = typeof(T).GetProperties().ToList();foreach (PropertyInfo prop in props){dt.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);}// 填充数据foreach (T item in items){DataRow row = dt.NewRow();foreach (PropertyInfo prop in props){row[prop.Name] = prop.GetValue(item, null) ?? DBNull.Value;}dt.Rows.Add(row);}return dt;}/// <summary>/// 转DataTable  泛型对象转换方法/// </summary>/// <typeparam name="T"></typeparam>/// <param name="items"></param>/// <param name="columnList">数据库中列集合</param>/// <returns></returns>public static DataTable ToDataTableSTMF<T>(this List<T> items, List<string> columnList){DataTable dt = new DataTable(typeof(T).Name);// 创建列var props = typeof(T).GetProperties().ToList();foreach (var colName in columnList){var prop = props.FirstOrDefault(t => t.Name == colName);dt.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);}int count = 0;// 填充数据foreach (T item in items){//if (count > 10)//{//    break;//}DataRow row = dt.NewRow();foreach (DataColumn itemColumn in dt.Columns){var prop = props.FirstOrDefault(t => t.Name == itemColumn.ColumnName);switch (prop.PropertyType.Name){case "Int32":case "Decimal":row[prop.Name] = prop.GetValue(item, null) ?? 0;break;case "String":row[prop.Name] = prop.GetValue(item, null) ?? string.Empty;break;case "Boolean":var valBit = prop.GetValue(item, null).ToBoolean();row[prop.Name] = valBit;break;default:row[prop.Name] = prop.GetValue(item, null) ?? string.Empty;break;}}#region MyRegion//foreach (PropertyInfo prop in props)//{//    if (!_insertExcludeColumns.Contains(prop.Name))//    {//        //row[prop.Name] = prop.GetValue(item, null) ?? DBNull.Value;//        switch (prop.PropertyType.Name)//        {//            case "Int32"://            case "Decimal"://                row[prop.Name] = prop.GetValue(item, null) ?? 0;//                break;//            case "String"://                row[prop.Name] = prop.GetValue(item, null) ?? string.Empty;//                break;//            case "Boolean"://                var valBit = prop.GetValue(item, null).ToBoolean();//                row[prop.Name] = valBit;//                break;//            default://                row[prop.Name] = prop.GetValue(item, null) ?? string.Empty;//                break;//        }//        //row[prop.Name] = prop.GetValue(item, null) ?? DBNull.Value;//    }//} #endregiondt.Rows.Add(row);count++;}return dt;}
}

http://www.lqws.cn/news/566893.html

相关文章:

  • 商业行业项目创业计划书PPT模版
  • 什么是区块链的跨链操作?
  • 穿越时空的光
  • 详解快速排序
  • SRS流媒体服务器(8)源码分析之rtc/rtmp互相转码详解
  • 数据可视化 - 单子图
  • 第10章 数组和指针
  • 左神算法之螺旋打印
  • SQL Server从入门到项目实践(超值版)读书笔记 19
  • 从GPTs到Real智能体:目前常见的几种创建智能体方式
  • spring:BeanPostProcessor后置处理器介绍
  • 小米路由器 AX3000T自定义子网掩码
  • Mybatis多条件查询设置参数的三种方法
  • stm32hal模块驱动(1)hpdl1414驱动
  • Vue的watch函数实现
  • 华为云 Flexus+DeepSeek 征文|华为云 Flexus 云服务 Dify-LLM 平台深度部署指南:从基础搭建到高可用实践
  • 智能制造——解读西门子数字化工厂规划报告(三年实施计划)【附全文阅读】
  • 机器学习在智能供应链中的应用:需求预测与库存优化
  • 大事件项目记录12-文章管理接口开发-总
  • 设计模式之适配器模式
  • OpenCV读取照片和可视化详解和代码示例
  • MySQL 安装使用教程
  • Java垃圾收集机制Test
  • PL-SLAM: Real-Time Monocular Visual SLAM with Points and Lines
  • Ai工具分享(2):Vscode+Cline无限免费的使用教程
  • XWPFDocument导出word文件
  • Linux中《动/静态库原理》
  • Redis缓存击穿深度解析:从现象到实战的完整解决方案
  • github上传代码步骤(http)
  • Cesium快速入门到精通系列教程十二:Cesium1.74中环绕地球生成​​经线环​​