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(); } }