当前位置: 首页> 财经> 访谈 > 集合扩展方法

集合扩展方法

时间:2025/7/17 0:32:50来源:https://blog.csdn.net/cjh16606260986/article/details/141941686 浏览次数:0次

功能清单

        1.判断集合是否为Null

        2.判断集合是否不为Null

        3.判断集合是否有值

        4.拼接成字符串

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

        6.LIST转DataTable

ToDataTable() 调用示例

从数据库查询出来的List<Model>数据导出成电子列表格式。 

Global.EAP_DataPath = string.Empty;
//焊接结果数据文件路径
var weldResultList = _serviceWeldResult.GetListByWhereExpr(t => t.MaterialsCode, Global.GlobalIns.MaterialsCodeCurrent);
if (weldResultList != null && weldResultList.Count > 0)
{//导出成文件var filePath = AppContext.BaseDirectory + @"WeldResultUpload\" + DateTime.Now.ToString("yyyyMMdd") + @"\" + Global.GlobalIns.MaterialsCodeCurrent + ".xlsx";if (filePath.CreateDirectoryByPath()){FileHelper.Export(weldResultList.ToDataTable(), filePath);Global.EAP_DataPath = filePath;//数据文件路径//Global.SecsServer.SetSvVal(EAPVariablesListEnum.DataPath.GetHashCode(), filePath);MessageHelper.ShowMsg("EAP上传焊接数据的模块码:" + Global.GlobalIns.MaterialsCodeCurrent + ",路径:" + Global.EAP_DataPath, method);//Global.SecsServer.SendEvent(EAPEventEnum.UploadData.GetHashCode());}
}
MessageHelper.ShowMsg("焊接结果数据发送给EAP完成", method);

源码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;namespace ExtendMethods;//
// 摘要:
//     集合扩展方法
public static class ListExtension
{//// 摘要://     判断集合是否为Null//// 参数://   list://// 类型参数://   T:public static bool IsNull<T>(this IEnumerable<T> list){return list == null;}//// 摘要://     判断集合是否不为Null//// 参数://   list://// 类型参数://   T:public static bool IsNotNull<T>(this IEnumerable<T> list){return list != null;}//// 摘要://     判断集合是否有值//// 参数://   list://// 类型参数://   T:public static bool IsHaveVal<T>(this IEnumerable<T> list){if (list != null){return list.Count() > 0;}return false;}//// 摘要://     拼接成字符串//// 参数://   list:////   split://     分隔筏public static string JoinToString(this IEnumerable<int> list, string split){if (list != null){return string.Join(split, list);}return string.Empty;}//// 摘要://     拼接成字符串//// 参数://   list:////   split://     分隔筏public static string JoinToString(this IEnumerable<string> list, string split){if (list != null){return string.Format("'{0}'", string.Join("'" + split + "'", list));}return string.Empty;}//// 摘要://     根据一个表达式去除重复//// 参数://   source://     数据源集合////   keySelector://     过滤表达式//// 类型参数://   TSource://     数据源类型////   TKey://     过滤属性类型public static IEnumerable<TSource> DistinctBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector){HashSet<TKey> seenKeys = new HashSet<TKey>();foreach (TSource item in source){if (seenKeys.Add(keySelector(item))){yield return item;}}}//// 摘要://     LIST转DataTable//// 参数://   data://// 类型参数://   T:public static DataTable ToDataTable<T>(this IList<T> data){DataTable dataTable = new DataTable();if (data != null && data.Count > 0){Dictionary<string, string> dictionary = new Dictionary<string, string>();PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(T));foreach (PropertyDescriptor item in properties){string text = item.Name;if (!string.IsNullOrEmpty(item.Description)){text = item.Description;}dataTable.Columns.Add(text, Nullable.GetUnderlyingType(item.PropertyType) ?? item.PropertyType);if (!dictionary.ContainsKey(item.Name)){dictionary.Add(item.Name, text);}}foreach (T datum in data){DataRow dataRow = dataTable.NewRow();foreach (PropertyDescriptor item2 in properties){string text2 = item2.Name;if (dictionary.ContainsKey(text2)){text2 = dictionary[item2.Name];}dataRow[text2] = item2.GetValue(datum) ?? DBNull.Value;}dataTable.Rows.Add(dataRow);}dictionary.Clear();}return dataTable;}
}

关键字:集合扩展方法

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: