功能清单
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;}
}