diff --git a/Falcon.SugarApi.Test/IEnumerableExtendTest.cs b/Falcon.SugarApi.Test/IEnumerableExtendTest.cs index f491e38..c2626bb 100644 --- a/Falcon.SugarApi.Test/IEnumerableExtendTest.cs +++ b/Falcon.SugarApi.Test/IEnumerableExtendTest.cs @@ -24,8 +24,8 @@ namespace Falcon.SugarApi.Test Console.WriteLine($"ToDataTable转换,共用时{sw.ElapsedMilliseconds}毫秒"); //测试表 Assert.IsNotNull(r); - Assert.AreEqual(3, r.Columns.Count); - Assert.AreEqual(2, r.Rows.Count); + Assert.AreEqual(3,r.Columns.Count); + Assert.AreEqual(2,r.Rows.Count); //测试列 var col1 = r.Columns[0]; Assert.IsTrue(col1.ColumnName == "a"); @@ -57,8 +57,8 @@ namespace Falcon.SugarApi.Test Console.WriteLine($"ToDataTable<>转换,共用时{sw.ElapsedMilliseconds}毫秒"); //测试表 Assert.IsNotNull(r); - Assert.AreEqual(2, r.Columns.Count); - Assert.AreEqual(2, r.Rows.Count); + Assert.AreEqual(2,r.Columns.Count); + Assert.AreEqual(2,r.Rows.Count); //测试列 col1 = r.Columns[0]; Assert.IsTrue(col1.ColumnName == "id"); @@ -80,9 +80,11 @@ 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, "对数组求和错误"); + 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 people = new List { new person{ IsMan=true,age=30 }, @@ -90,22 +92,33 @@ 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, "有条件求和错误"); + 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) { + var men = people.Reduce((a,b) => { + if(b.IsMan) { a.Add(b); } return a; - }, new List()); - foreach (var p in men) { - Assert.IsTrue(p.IsMan, "缩减为男性集合错误!"); + },new List()); + foreach(var p in men) { + Assert.IsTrue(p.IsMan,"缩减为男性集合错误!"); + } + men = people.Reduce(new List(),(a,b) => { + if(b.IsMan) { + a.Add(b); + } + return a; + }); + foreach(var p in men) { + Assert.IsTrue(p.IsMan,"缩减为男性集合错误!"); } var arr = new string[] { }; - var initVal = arr.Reduce((a, b) => throw new Exception("空集合不可以调用缩减方法"), "abc"); - Assert.IsTrue(initVal == "abc", "空集合返回初始值,并且不调用缩减方法。"); + var initVal = arr.Reduce((a,b) => throw new Exception("空集合不可以调用缩减方法"),"abc"); + Assert.IsTrue(initVal == "abc","空集合返回初始值,并且不调用缩减方法。"); } } diff --git a/Falcon.SugarApi/IEnumerableExtend.cs b/Falcon.SugarApi/IEnumerableExtend.cs index 5d92872..67c82ce 100644 --- a/Falcon.SugarApi/IEnumerableExtend.cs +++ b/Falcon.SugarApi/IEnumerableExtend.cs @@ -20,13 +20,13 @@ namespace Falcon.SugarApi public static DataTable ToDataTable(this IEnumerable 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 < source.Count();i++) { var item = source.ToArray()[i]; - foreach (PropertyInfo pro in item.GetType().GetProperties()) { - if (!dt.Columns.Contains(pro.Name)) { + foreach(PropertyInfo pro in item.GetType().GetProperties()) { + if(!dt.Columns.Contains(pro.Name)) { dt.Columns.Add(new DataColumn { ColumnName = pro.Name, DataType = pro.PropertyType, @@ -34,16 +34,15 @@ namespace Falcon.SugarApi } } } - foreach (var i in source) { + foreach(var i in source) { var row = dt.NewRow(); - foreach (var p in i.GetType().GetProperties()) { - if (p.CanRead) { + 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); + } catch(Exception ex) { + throw new Exception($"值设置失败!{p.Name}:{val}",ex); } } } @@ -64,18 +63,18 @@ namespace Falcon.SugarApi var type = typeof(T); var dt = new DataTable(); var pros = typeof(T).GetProperties(); - foreach (PropertyInfo p in pros) { - if (p.CanRead) { + foreach(PropertyInfo p in pros) { + if(p.CanRead) { dt.Columns.Add(new DataColumn { ColumnName = p.Name, DataType = p.PropertyType, }); } } - foreach (var i in source) { + foreach(var i in source) { var row = dt.NewRow(); - foreach (var p in pros) { - if (p.CanRead) { + foreach(var p in pros) { + if(p.CanRead) { var val = p.GetValue(i); row[p.Name] = val; } @@ -94,13 +93,33 @@ namespace Falcon.SugarApi /// 缩减方法 /// 缩减初始值 /// 缩减结果 - public static TR Reduce(this IEnumerable source, Func reduceFunc, TR initialValue) { - if (reduceFunc == null) { + public static TR Reduce(this IEnumerable source,Func reduceFunc,TR initialValue) { + if(reduceFunc == null) { throw new ArgumentNullException(nameof(reduceFunc)); } var result = initialValue; - foreach (var i in source) { - result = reduceFunc(result, i); + foreach(var i in source) { + result = reduceFunc(result,i); + } + return result; + } + + /// + /// 对枚举进行缩减,并返回缩减后的结果。 + /// + /// 枚举的类型 + /// 缩减结果类型 + /// 原枚举 + /// 缩减方法 + /// 缩减初始值 + /// 缩减结果 + public static TR Reduce(this IEnumerable source,TR initialValue,Func reduceFunc) { + if(reduceFunc == null) { + throw new ArgumentNullException(nameof(reduceFunc)); + } + var result = initialValue; + foreach(var i in source) { + result = reduceFunc(result,i); } return result; }