From 2e1a0ad0c0015e3eeed28c666fa9af153cd6fec6 Mon Sep 17 00:00:00 2001 From: falcon <9504402@qq.com> Date: Wed, 4 May 2022 16:11:18 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0IEnumerable<>=E6=89=A9?= =?UTF-8?q?=E5=B1=95=E6=96=B9=E6=B3=95=EF=BC=8C=E5=AE=9E=E7=8E=B0=E6=9E=9A?= =?UTF-8?q?=E4=B8=BE=E7=B1=BB=E5=9E=8B=E5=88=B0DataTable=E7=9A=84=E8=BD=AC?= =?UTF-8?q?=E6=8D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Falcon.SugarApi.Test/IEnumerableExtendTest.cs | 89 +++++++++++++++++++ Falcon.SugarApi/IEnumerableExtend.cs | 88 ++++++++++++++++++ README.md | 24 ++--- 3 files changed, 191 insertions(+), 10 deletions(-) create mode 100644 Falcon.SugarApi.Test/IEnumerableExtendTest.cs create mode 100644 Falcon.SugarApi/IEnumerableExtend.cs diff --git a/Falcon.SugarApi.Test/IEnumerableExtendTest.cs b/Falcon.SugarApi.Test/IEnumerableExtendTest.cs new file mode 100644 index 0000000..7f7cd7a --- /dev/null +++ b/Falcon.SugarApi.Test/IEnumerableExtendTest.cs @@ -0,0 +1,89 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System; +using System.Collections.Generic; +using System.Diagnostics; + +namespace Falcon.SugarApi.Test +{ + /// + /// 枚举类型扩展方法 + /// + [TestClass] + public class IEnumerableExtendTest + { + [TestMethod("ToDataTable测试")] + public void ToDataTableTest() { + var sw = new Stopwatch(); + var list = new List { + new{a=1,b="b1" }, + new{a=2,b="b2",c=1 }, + }; + sw.Start(); + var r = list.ToDataTable(); + sw.Stop(); + Console.WriteLine($"ToDataTable转换,共用时{sw.ElapsedMilliseconds}毫秒"); + //测试表 + Assert.IsNotNull(r); + Assert.AreEqual(3, r.Columns.Count); + Assert.AreEqual(2, r.Rows.Count); + //测试列 + var col1 = r.Columns[0]; + Assert.IsTrue(col1.ColumnName == "a"); + Assert.IsTrue(col1.DataType == typeof(int)); + var col2 = r.Columns[1]; + Assert.IsTrue(col2.ColumnName == "b"); + Assert.IsTrue(col2.DataType == typeof(string)); + var col3 = r.Columns[2]; + Assert.IsTrue(col3.ColumnName == "c"); + Assert.IsTrue(col3.DataType == typeof(int)); + //测试行 + var row = r.Rows[0]; + Assert.IsTrue(row[0].ToString() == "1"); + Assert.IsTrue(row[1].ToString() == "b1"); + Assert.IsTrue(row[2].ToString() == ""); + row = r.Rows[1]; + Assert.IsTrue((int)row[0] == 2); + Assert.IsTrue((string)row[1] == "b2"); + Assert.IsTrue((int)row[2] == 1); + Console.WriteLine($"ToDataTable测试完成"); + + var models = new List { + new ToDataTableTestModel{ id=1, }, + new ToDataTableTestModel{id=2,Name="name2" }, + }; + sw.Reset(); + r = models.ToDataTable(); + sw.Stop(); + Console.WriteLine($"ToDataTable<>转换,共用时{sw.ElapsedMilliseconds}毫秒"); + //测试表 + Assert.IsNotNull(r); + Assert.AreEqual(2, r.Columns.Count); + Assert.AreEqual(2, r.Rows.Count); + //测试列 + col1 = r.Columns[0]; + Assert.IsTrue(col1.ColumnName == "id"); + Assert.IsTrue(col1.DataType == typeof(int)); + col2 = r.Columns[1]; + Assert.IsTrue(col2.ColumnName == "Name"); + Assert.IsTrue(col2.DataType == typeof(string)); + //测试行 + row = r.Rows[0]; + Assert.IsTrue(row[0].ToString() == "1"); + Assert.IsTrue(row[1].ToString() == ""); + row = r.Rows[1]; + Assert.IsTrue((int)row[0] == 2); + Assert.IsTrue(row[1].ToString() == "name2"); + Console.WriteLine($"ToDataTable<>测试完成"); + + + } + } + + internal class ToDataTableTestModel + { + public int id { get; set; } + public string Name { get; set; } + } + +} + diff --git a/Falcon.SugarApi/IEnumerableExtend.cs b/Falcon.SugarApi/IEnumerableExtend.cs new file mode 100644 index 0000000..3011029 --- /dev/null +++ b/Falcon.SugarApi/IEnumerableExtend.cs @@ -0,0 +1,88 @@ +using System; +using System.Collections.Generic; +using System.Data; +using System.Linq; +using System.Reflection; + +namespace Falcon.SugarApi +{ + /// + /// 枚举扩展 + /// + public static class IEnumerableExtend + { + /// + /// 枚举转换为DataTable + /// + /// 数据枚举 + /// 转换后Datatable + /// 参数为Null + public static DataTable ToDataTable(this IEnumerable source) { + _ = source ?? throw new ArgumentNullException(nameof(source)); + var dt = new DataTable(); + if (source.Count() == 0) { + return dt; + } + for (int i = 0; i < source.Count(); i++) { + var item = source.ToArray()[i]; + foreach (PropertyInfo pro in item.GetType().GetProperties()) { + if (!dt.Columns.Contains(pro.Name)) { + dt.Columns.Add(new DataColumn { + ColumnName = pro.Name, + DataType = pro.PropertyType, + }); + } + } + } + foreach (var i in source) { + var row = dt.NewRow(); + foreach (var p in i.GetType().GetProperties()) { + if (p.CanRead) { + var val = p.GetValue(i); + try { + row[p.Name] = val; + } + catch (Exception ex) { + throw new Exception($"值设置失败!{p.Name}:{val}", ex); + } + } + } + dt.Rows.Add(row); + } + return dt; + } + + /// + /// 枚举转换为DataTable + /// + /// 枚举的元素类型 + /// 原数据 + /// 转换后Datatable + /// 参数为Null + public static DataTable ToDataTable(this IEnumerable source) { + _ = source ?? throw new ArgumentNullException(nameof(source)); + var type = typeof(T); + var dt = new DataTable(); + var pros = typeof(T).GetProperties(); + foreach (PropertyInfo p in pros) { + if (p.CanRead) { + dt.Columns.Add(new DataColumn { + ColumnName = p.Name, + DataType = p.PropertyType, + }); + } + } + foreach (var i in source) { + var row = dt.NewRow(); + foreach (var p in pros) { + if (p.CanRead) { + var val = p.GetValue(i); + row[p.Name] = val; + } + } + dt.Rows.Add(row); + } + return dt; + } + } +} diff --git a/README.md b/README.md index 855c801..6128661 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ ## Falcon.SugarApi ### WebApiչģ [](/Falcon/Falcon.SugarApi/src/branch/master/Falcon.SugarApi/ApiDefinistions) -> `ApiControllerBase`webapi࣬ʵݿ⡢log쳣Ȼܡ +> `ApiControllerBase`webapi࣬ʵݿ⡢log쳣Ȼܡ > `ApiBaseOptionController<>`лwebapi࣬`ApiControllerBase`⻹ṩɾIJ鹦ܡ ### Swaggerչģ [](/Falcon/Falcon.SugarApi/src/branch/master/Falcon.SugarApi/Swagger) @@ -13,6 +13,17 @@ > `SugarDbTables`ݿϻ࣬Լ̳иಢж`DbSet`ԣЩԻԶʵ > `SugarTableBase`׼̳࣬иṩݱܣID޸ʱȡ +### ̨BackTaskģ [](/Falcon/Falcon.SugarApi/src/branch/master/Falcon.SugarApi/BackTask) +> ̳`BackgroundLongTask`ʵһλںִ̨еĶ󣬸ͨ`IServiceCollection.AddHostedService<>'עᡣ +> `RunTimespan`ִеʱ +> д`RunAsync`ʵһ첽 +> д`OnStart`̨ʼ`OnStop`̨`OnCompleted`һִɺ`OnException`ִδ쳣 + +### ݻģ [](/Falcon/Falcon.SugarApi/src/branch/master/Falcon.SugarApi/Cache) +> ͨ`IServiceCollection.AddSugarRedisCache` `IServiceCollection.AddSugarMemoryCache`עỺ +> ͨע `ISugarCache`ͬӿ `ISugarCacheAsync`첽ӿڻȡ +> ͨӿڵ`Get``Set`ȡͱ滺ֵ + ### XMLлչģ [](/Falcon/Falcon.SugarApi/src/branch/master/Falcon.SugarApi/XmlSerialize) > `IServiceCollection.AddXmlSerializeFactory`עxmlл`XmlSerializeFactory`ͨùʵһXMLл @@ -26,13 +37,6 @@ ### Objectչ > `CloneTo``CloneFrom`ʵֽԸֵĿͬУΪdzơ -### ̨BackTaskģ [](/Falcon/Falcon.SugarApi/src/branch/master/Falcon.SugarApi/BackTask) -> ̳`BackgroundLongTask`ʵһλںִ̨еĶ󣬸ͨ`IServiceCollection.AddHostedService<>'עᡣ -> `RunTimespan`ִеʱ -> д`RunAsync`ʵһ첽 -> д`OnStart`̨ʼ`OnStop`̨`OnCompleted`һִɺ`OnException`ִδ쳣 +### IEnumerableչ +> `ToDataTable``ToDataTable<>`ֱʵֽöתΪDataTableķһObjectöһԾö٣ʹ÷Ͱ汾ӸЧ -### ݻģ [](/Falcon/Falcon.SugarApi/src/branch/master/Falcon.SugarApi/Cache) -> ͨ`IServiceCollection.AddSugarRedisCache` `IServiceCollection.AddSugarMemoryCache`עỺ -> ͨע `ISugarCache`ͬӿ `ISugarCacheAsync`첽ӿڻȡ -> ͨӿڵ`Get``Set`ȡͱ滺ֵ \ No newline at end of file