DataTableHelp.cs 3.57 KB
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;
using System.Data;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Xml.Serialization;

namespace Hh.Mes.Common.DataTableTo
{
    /// <summary>
    /// DataTableHelp 的摘要说明
    /// </summary>
    public static class DataTableHelp
    {
        /// <summary>
        /// 获取某一列的所有值 (字符串拼接手动 string.Join(",", x))
        /// </summary>
        /// <typeparam name="T">列数据类型</typeparam>
        /// <param name="dtSource">数据表</param>
        /// <param name="filedName">列名</param>
        /// <returns></returns>
        public static List<T> GetColumnValues<T>(this DataTable dtSource, string filedName)
        {
            return (from r in dtSource.AsEnumerable() select r.Field<T>(filedName)).ToList();
        }


        /// <summary>
        /// 去重复数据方法
        /// </summary>
        public static DataTable Distinct(this DataTable dtSource, params string[] filedNames)
        {
          return  dtSource.DefaultView.ToTable(true, filedNames); 
        }

        /// <summary>
        /// dt判断是否为null 或者 行数是否为0,true是为空,false存在数据
        /// </summary>
        public static bool IsEmpty(this DataTable dt)
        {
            return dt == null || dt.Rows.Count <= 0;
        }

        /// <summary>
        /// string 空返回0,
        /// </summary>
        public static string IsEmpty(this object val, string retVal = "")
        {
            if (val == null) return "";
            var temp = val.ToString();
            return string.IsNullOrEmpty(temp) ? retVal : temp;
        }

        /// <summary>
        /// list转datatable  DataTableHelp.ListToDt(list)
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="collection"></param>
        /// <returns></returns>
        public static DataTable ListToDt<T>(IEnumerable<T> collection)
        {
            var props = typeof(T).GetProperties().Where(t => t.GetCustomAttribute(typeof(EditableAttribute)) == null);
            var dt = new DataTable();
            foreach (var item in props)
            {
                DataColumn dataColumn;
                Type colType = item.PropertyType;
                if (colType.IsGenericType && colType.GetGenericTypeDefinition() == typeof(Nullable<>))
                {
                    colType = colType.GetGenericArguments()[0];
                }
                var attr = (DescriptionAttribute)item.GetCustomAttribute(typeof(DescriptionAttribute));
                if (attr != null)
                {
                    dataColumn = new DataColumn(attr.Description, colType);
                }
                else
                {
                    dataColumn = new DataColumn(item.Name, colType);
                }
                dt.Columns.Add(dataColumn);
            }
            if (collection.Count() > 0)
            {
                for (int i = 0; i < collection.Count(); i++)
                {
                    ArrayList tempList = new ArrayList();
                    foreach (PropertyInfo pi in props)
                    {
                        object obj = pi.GetValue(collection.ElementAt(i), null);
                        tempList.Add(obj);
                    }
                    object[] array = tempList.ToArray();
                    dt.LoadDataRow(array, true);
                }
            }
            return dt;
        }
    }
}