From f5a44f956952ee7411a2169e4a83f5d3e34fb412 Mon Sep 17 00:00:00 2001 From: falcon <9504402@qq.com> Date: Thu, 2 Mar 2023 10:38:38 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9E=9A=E4=B8=BE=E7=B1=BB=E5=9E=8B=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E4=BA=86IsNullOrEmpty=E6=96=B9=E6=B3=95=EF=BC=8C?= =?UTF-8?q?=E5=88=A4=E6=96=AD=E6=9E=9A=E4=B8=BE=E6=98=AF=E5=90=A6=E4=B8=BA?= =?UTF-8?q?null=E6=88=96=E8=80=85=E6=B2=A1=E6=9C=89=E4=BB=BB=E4=BD=95?= =?UTF-8?q?=E5=85=83=E7=B4=A0=EF=BC=8C=E5=B9=B6=E8=BF=9B=E8=A1=8C=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Falcon.SugarApi.Test/IEnumerableExtendTest.cs | 85 +++++++++++-------- Falcon.SugarApi/IEnumerableExtend.cs | 51 +++++++---- 2 files changed, 85 insertions(+), 51 deletions(-) diff --git a/Falcon.SugarApi.Test/IEnumerableExtendTest.cs b/Falcon.SugarApi.Test/IEnumerableExtendTest.cs index c2626bb..0d475b9 100644 --- a/Falcon.SugarApi.Test/IEnumerableExtendTest.cs +++ b/Falcon.SugarApi.Test/IEnumerableExtendTest.cs @@ -28,23 +28,23 @@ namespace Falcon.SugarApi.Test Assert.AreEqual(2,r.Rows.Count); //测试列 var col1 = r.Columns[0]; - Assert.IsTrue(col1.ColumnName == "a"); - Assert.IsTrue(col1.DataType == typeof(int)); + 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)); + 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)); + 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); + 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 { @@ -52,7 +52,7 @@ namespace Falcon.SugarApi.Test new ToDataTableTestModel{id=2,Name="name2" }, }; sw.Reset(); - r = models.ToDataTable(); + r=models.ToDataTable(); sw.Stop(); Console.WriteLine($"ToDataTable<>转换,共用时{sw.ElapsedMilliseconds}毫秒"); //测试表 @@ -60,19 +60,19 @@ namespace Falcon.SugarApi.Test 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)); + 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"); + 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<>测试完成"); @@ -81,10 +81,10 @@ namespace Falcon.SugarApi.Test [TestMethod("Reduce测试")] public void ReduceTest() { var list = new List { 1,2,3,4 }; - var sum = list.Reduce((a,b) => a + b,0); - Assert.IsTrue(sum == 10,"对数组求和错误"); - sum = list.Reduce(0,(a,b) => a + b); - Assert.IsTrue(sum == 10,"对数组求和错误"); + var sum = list.Reduce((a,b) => a+b,0); + Assert.IsTrue(sum==10,"对数组求和错误"); + sum=list.Reduce(0,(a,b) => a+b); + Assert.IsTrue(sum==10,"对数组求和错误"); var people = new List { new person{ IsMan=true,age=30 }, @@ -92,10 +92,10 @@ namespace Falcon.SugarApi.Test new person{ IsMan=true,age=50 }, new person{ IsMan=true,age=60 }, }; - var sumage = people.Reduce((a,b) => a + (b.IsMan ? b.age : 0),0); - Assert.IsTrue(sumage == 30 + 50 + 60,"有条件求和错误"); - sumage = people.Reduce(0,(a,b) => b.IsMan ? a + b.age : a); - Assert.IsTrue(sumage == 30 + 50 + 60,"有条件求和错误"); + var sumage = people.Reduce((a,b) => a+(b.IsMan ? b.age : 0),0); + Assert.IsTrue(sumage==30+50+60,"有条件求和错误"); + sumage=people.Reduce(0,(a,b) => b.IsMan ? a+b.age : a); + Assert.IsTrue(sumage==30+50+60,"有条件求和错误"); var men = people.Reduce((a,b) => { if(b.IsMan) { @@ -106,7 +106,7 @@ namespace Falcon.SugarApi.Test foreach(var p in men) { Assert.IsTrue(p.IsMan,"缩减为男性集合错误!"); } - men = people.Reduce(new List(),(a,b) => { + men=people.Reduce(new List(),(a,b) => { if(b.IsMan) { a.Add(b); } @@ -118,7 +118,20 @@ namespace Falcon.SugarApi.Test var arr = new string[] { }; var initVal = arr.Reduce((a,b) => throw new Exception("空集合不可以调用缩减方法"),"abc"); - Assert.IsTrue(initVal == "abc","空集合返回初始值,并且不调用缩减方法。"); + Assert.IsTrue(initVal=="abc","空集合返回初始值,并且不调用缩减方法。"); + } + + [TestMethod("IsNullOrEmpty测试")] + public void IsNullOrEmptyTest() { + List list = null; + Assert.IsTrue(list.IsNullOrEmpty()); + Assert.IsFalse(list.IsNotNullOrEmpty()); + list=new List(); + Assert.IsTrue(list.IsNullOrEmpty()); + Assert.IsFalse(list.IsNotNullOrEmpty()); + list.Add(1); + Assert.IsFalse(list.IsNullOrEmpty()); + Assert.IsTrue(list.IsNotNullOrEmpty()); } } diff --git a/Falcon.SugarApi/IEnumerableExtend.cs b/Falcon.SugarApi/IEnumerableExtend.cs index 67c82ce..c1eeb16 100644 --- a/Falcon.SugarApi/IEnumerableExtend.cs +++ b/Falcon.SugarApi/IEnumerableExtend.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Data; +using System.Diagnostics.CodeAnalysis; using System.Linq; using System.Reflection; @@ -18,18 +19,18 @@ namespace Falcon.SugarApi /// 转换后Datatable /// 参数为Null public static DataTable ToDataTable(this IEnumerable source) { - _ = source ?? throw new ArgumentNullException(nameof(source)); + _=source??throw new ArgumentNullException(nameof(source)); var dt = new DataTable(); - if(source.Count() == 0) { + if(source.Count()==0) { return dt; } - for(int i = 0;i < source.Count();i++) { + for(int i = 0;i转换后Datatable /// 参数为Null public static DataTable ToDataTable(this IEnumerable source) { - _ = source ?? throw new ArgumentNullException(nameof(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, + ColumnName=p.Name, + DataType=p.PropertyType, }); } } @@ -76,7 +78,7 @@ namespace Falcon.SugarApi foreach(var p in pros) { if(p.CanRead) { var val = p.GetValue(i); - row[p.Name] = val; + row[p.Name]=val; } } dt.Rows.Add(row); @@ -94,12 +96,12 @@ namespace Falcon.SugarApi /// 缩减初始值 /// 缩减结果 public static TR Reduce(this IEnumerable source,Func reduceFunc,TR initialValue) { - if(reduceFunc == null) { + if(reduceFunc==null) { throw new ArgumentNullException(nameof(reduceFunc)); } var result = initialValue; foreach(var i in source) { - result = reduceFunc(result,i); + result=reduceFunc(result,i); } return result; } @@ -114,14 +116,33 @@ namespace Falcon.SugarApi /// 缩减初始值 /// 缩减结果 public static TR Reduce(this IEnumerable source,TR initialValue,Func reduceFunc) { - if(reduceFunc == null) { + if(reduceFunc==null) { throw new ArgumentNullException(nameof(reduceFunc)); } var result = initialValue; + if(source.IsNull()) { + + } foreach(var i in source) { - result = reduceFunc(result,i); + result=reduceFunc(result,i); } return result; } + + /// + /// 返回枚举是否为null或者集合无元素 + /// + /// 枚举的元素类型 + /// 枚举对象 + /// 为null或者无元素返回True,否则False + public static bool IsNullOrEmpty([AllowNull] this IEnumerable values) => values==null||values.Count()==0; + + /// + /// 返回枚举是否不为null或者集合无元素,结果是对IsNullOrEmpty去反。 + /// + /// 枚举的元素类型 + /// 枚举对象 + /// 枚举不为null或者无元素返回True,否则False + public static bool IsNotNullOrEmpty([AllowNull] this IEnumerable values) => !values.IsNullOrEmpty(); } }