Browse Source

datatable

懒得勤快 6 years ago
parent
commit
fc28656f91

+ 11 - 46
Masuit.Tools.Core/Database/DataTableHelper.cs

@@ -1,6 +1,7 @@
 using System;
 using System.Collections;
 using System.Collections.Generic;
+using System.ComponentModel;
 using System.Data;
 using System.Reflection;
 
@@ -72,72 +73,36 @@ namespace Masuit.Tools.Core.Database
         /// </summary>
         /// <typeparam name="T">集合项类型</typeparam>
         /// <param name="list">集合</param>
+        /// <param name="tableName">表名</param>
         /// <returns>数据集(表)</returns>
-        public static DataTable ToDataTable<T>(this IList<T> list)
+        public static DataTable ToDataTable<T>(this IList<T> list, string tableName)
         {
-            return ToDataTable(list, null);
-        }
-
-        /// <summary>
-        /// 将泛型集合类转换成DataTable
-        /// </summary>
-        /// <typeparam name="T">集合项类型</typeparam>
-        /// <param name="list">集合</param>
-        /// <param name="propertyName">需要返回的列的列名</param>
-        /// <returns>数据集(表)</returns>
-        public static DataTable ToDataTable<T>(this IList<T> list, params string[] propertyName)
-        {
-            List<string> propertyNameList = new List<string>();
-            if (propertyName != null)
-            {
-                propertyNameList.AddRange(propertyName);
-            }
-
             DataTable result = new DataTable();
             if (list.Count <= 0)
             {
                 return result;
             }
 
-            var propertys = list[0].GetType().GetProperties();
-            propertys.ForEach(pi =>
+            var properties = list[0].GetType().GetProperties();
+            properties.ForEach(pi =>
             {
-                if (propertyNameList.Count == 0)
-                {
-                    result.Columns.Add(pi.Name, pi.PropertyType);
-                }
-                else
-                {
-                    if (propertyNameList.Contains(pi.Name))
-                    {
-                        result.Columns.Add(pi.Name, pi.PropertyType);
-                    }
-                }
+                var columnName = pi.GetCustomAttribute<DescriptionAttribute>()?.Description ?? pi.Name;
+                result.Columns.Add(columnName, pi.PropertyType);
             });
             list.ForEach(item =>
             {
                 ArrayList tempList = new ArrayList();
-                foreach (PropertyInfo pi in propertys)
+                foreach (PropertyInfo pi in properties)
                 {
-                    if (propertyNameList.Count == 0)
-                    {
-                        object obj = pi.GetValue(item, null);
-                        tempList.Add(obj);
-                    }
-                    else
-                    {
-                        if (propertyNameList.Contains(pi.Name))
-                        {
-                            object obj = pi.GetValue(item, null);
-                            tempList.Add(obj);
-                        }
-                    }
+                    object obj = pi.GetValue(item, null);
+                    tempList.Add(obj);
                 }
 
                 object[] array = tempList.ToArray();
                 result.LoadDataRow(array, true);
             });
 
+            result.TableName = tableName;
             return result;
         }
 

+ 2 - 10
Masuit.Tools.Core/Masuit.Tools.Core.xml

@@ -521,21 +521,13 @@
             <param name="dt"></param>
             <returns></returns>
         </member>
-        <member name="M:Masuit.Tools.Core.Database.DataTableHelper.ToDataTable``1(System.Collections.Generic.IList{``0})">
+        <member name="M:Masuit.Tools.Core.Database.DataTableHelper.ToDataTable``1(System.Collections.Generic.IList{``0},System.String)">
             <summary>
             将泛型集合类转换成DataTable
             </summary>
             <typeparam name="T">集合项类型</typeparam>
             <param name="list">集合</param>
-            <returns>数据集(表)</returns>
-        </member>
-        <member name="M:Masuit.Tools.Core.Database.DataTableHelper.ToDataTable``1(System.Collections.Generic.IList{``0},System.String[])">
-            <summary>
-            将泛型集合类转换成DataTable
-            </summary>
-            <typeparam name="T">集合项类型</typeparam>
-            <param name="list">集合</param>
-            <param name="propertyName">需要返回的列的列名</param>
+            <param name="tableName">表名</param>
             <returns>数据集(表)</returns>
         </member>
         <member name="M:Masuit.Tools.Core.Database.DataTableHelper.CreateTable(System.Collections.Generic.List{System.String})">

+ 10 - 46
Masuit.Tools/Database/DataTableHelper.cs

@@ -72,72 +72,36 @@ namespace Masuit.Tools.Database
         /// </summary>
         /// <typeparam name="T">集合项类型</typeparam>
         /// <param name="list">集合</param>
+        /// <param name="tableName">表名</param>
         /// <returns>数据集(表)</returns>
-        public static DataTable ToDataTable<T>(this IList<T> list)
+        public static DataTable ToDataTable<T>(this IList<T> list, string tableName)
         {
-            return ToDataTable(list, null);
-        }
-
-        /// <summary>
-        /// 将泛型集合类转换成DataTable
-        /// </summary>
-        /// <typeparam name="T">集合项类型</typeparam>
-        /// <param name="list">集合</param>
-        /// <param name="propertyName">需要返回的列的列名</param>
-        /// <returns>数据集(表)</returns>
-        public static DataTable ToDataTable<T>(this IList<T> list, params string[] propertyName)
-        {
-            List<string> propertyNameList = new List<string>();
-            if (propertyName != null)
-            {
-                propertyNameList.AddRange(propertyName);
-            }
-
             DataTable result = new DataTable();
             if (list.Count <= 0)
             {
                 return result;
             }
 
-            var propertys = list[0].GetType().GetProperties();
-            propertys.ForEach(pi =>
+            var properties = list[0].GetType().GetProperties();
+            properties.ForEach(pi =>
             {
-                if (propertyNameList.Count == 0)
-                {
-                    result.Columns.Add(pi.Name, pi.PropertyType);
-                }
-                else
-                {
-                    if (propertyNameList.Contains(pi.Name))
-                    {
-                        result.Columns.Add(pi.Name, pi.PropertyType);
-                    }
-                }
+                var columnName = pi.GetCustomAttribute<DescriptionAttribute>()?.Description ?? pi.Name;
+                result.Columns.Add(columnName, pi.PropertyType);
             });
             list.ForEach(item =>
             {
                 ArrayList tempList = new ArrayList();
-                foreach (PropertyInfo pi in propertys)
+                foreach (PropertyInfo pi in properties)
                 {
-                    if (propertyNameList.Count == 0)
-                    {
-                        object obj = pi.GetValue(item, null);
-                        tempList.Add(obj);
-                    }
-                    else
-                    {
-                        if (propertyNameList.Contains(pi.Name))
-                        {
-                            object obj = pi.GetValue(item, null);
-                            tempList.Add(obj);
-                        }
-                    }
+                    object obj = pi.GetValue(item, null);
+                    tempList.Add(obj);
                 }
 
                 object[] array = tempList.ToArray();
                 result.LoadDataRow(array, true);
             });
 
+            result.TableName = tableName;
             return result;
         }
 

+ 9 - 0
NetCoreTest/Program.cs

@@ -2,6 +2,7 @@
 using Microsoft.AspNetCore;
 using Microsoft.AspNetCore.Hosting;
 using System;
+using System.ComponentModel;
 
 namespace NetCoreTest
 {
@@ -18,4 +19,12 @@ namespace NetCoreTest
                 .UseStartup<Startup>();
 
     }
+
+    public class MyClass
+    {
+        [Description("test")]
+        public string MyProperty { get; set; }
+        public int MyProperty1 { get; set; }
+
+    }
 }