新增对IEnumerable对象的Reduce可以先提供初始值后提供缩减方法的重载

This commit is contained in:
falcon 2022-12-07 10:36:42 +08:00
parent ca1209c310
commit ce77072812
2 changed files with 67 additions and 35 deletions

View File

@ -24,8 +24,8 @@ namespace Falcon.SugarApi.Test
Console.WriteLine($"ToDataTable转换共用时{sw.ElapsedMilliseconds}毫秒"); Console.WriteLine($"ToDataTable转换共用时{sw.ElapsedMilliseconds}毫秒");
//测试表 //测试表
Assert.IsNotNull(r); Assert.IsNotNull(r);
Assert.AreEqual(3, r.Columns.Count); Assert.AreEqual(3,r.Columns.Count);
Assert.AreEqual(2, r.Rows.Count); Assert.AreEqual(2,r.Rows.Count);
//测试列 //测试列
var col1 = r.Columns[0]; var col1 = r.Columns[0];
Assert.IsTrue(col1.ColumnName == "a"); Assert.IsTrue(col1.ColumnName == "a");
@ -57,8 +57,8 @@ namespace Falcon.SugarApi.Test
Console.WriteLine($"ToDataTable<>转换,共用时{sw.ElapsedMilliseconds}毫秒"); Console.WriteLine($"ToDataTable<>转换,共用时{sw.ElapsedMilliseconds}毫秒");
//测试表 //测试表
Assert.IsNotNull(r); Assert.IsNotNull(r);
Assert.AreEqual(2, r.Columns.Count); Assert.AreEqual(2,r.Columns.Count);
Assert.AreEqual(2, r.Rows.Count); Assert.AreEqual(2,r.Rows.Count);
//测试列 //测试列
col1 = r.Columns[0]; col1 = r.Columns[0];
Assert.IsTrue(col1.ColumnName == "id"); Assert.IsTrue(col1.ColumnName == "id");
@ -80,9 +80,11 @@ namespace Falcon.SugarApi.Test
[TestMethod("Reduce测试")] [TestMethod("Reduce测试")]
public void ReduceTest() { public void ReduceTest() {
var list = new List<int> { 1, 2, 3, 4 }; var list = new List<int> { 1,2,3,4 };
var sum = list.Reduce((a, b) => a + b, 0); var sum = list.Reduce((a,b) => a + b,0);
Assert.IsTrue(sum == 10, "对数组求和错误"); Assert.IsTrue(sum == 10,"对数组求和错误");
sum = list.Reduce(0,(a,b) => a + b);
Assert.IsTrue(sum == 10,"对数组求和错误");
var people = new List<person> { var people = new List<person> {
new person{ IsMan=true,age=30 }, 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=50 },
new person{ IsMan=true,age=60 }, new person{ IsMan=true,age=60 },
}; };
var sumage = people.Reduce((a, b) => a + (b.IsMan ? b.age : 0), 0); var sumage = people.Reduce((a,b) => a + (b.IsMan ? b.age : 0),0);
Assert.IsTrue(sumage == 30 + 50 + 60, "有条件求和错误"); 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) => { var men = people.Reduce((a,b) => {
if (b.IsMan) { if(b.IsMan) {
a.Add(b); a.Add(b);
} }
return a; return a;
}, new List<person>()); },new List<person>());
foreach (var p in men) { foreach(var p in men) {
Assert.IsTrue(p.IsMan, "缩减为男性集合错误!"); Assert.IsTrue(p.IsMan,"缩减为男性集合错误!");
}
men = people.Reduce(new List<person>(),(a,b) => {
if(b.IsMan) {
a.Add(b);
}
return a;
});
foreach(var p in men) {
Assert.IsTrue(p.IsMan,"缩减为男性集合错误!");
} }
var arr = new string[] { }; var arr = new string[] { };
var initVal = arr.Reduce((a, b) => throw new Exception("空集合不可以调用缩减方法"), "abc"); var initVal = arr.Reduce((a,b) => throw new Exception("空集合不可以调用缩减方法"),"abc");
Assert.IsTrue(initVal == "abc", "空集合返回初始值,并且不调用缩减方法。"); Assert.IsTrue(initVal == "abc","空集合返回初始值,并且不调用缩减方法。");
} }
} }

View File

@ -20,13 +20,13 @@ namespace Falcon.SugarApi
public static DataTable ToDataTable(this IEnumerable<object> source) { public static DataTable ToDataTable(this IEnumerable<object> source) {
_ = source ?? throw new ArgumentNullException(nameof(source)); _ = source ?? throw new ArgumentNullException(nameof(source));
var dt = new DataTable(); var dt = new DataTable();
if (source.Count() == 0) { if(source.Count() == 0) {
return dt; return dt;
} }
for (int i = 0; i < source.Count(); i++) { for(int i = 0;i < source.Count();i++) {
var item = source.ToArray()[i]; var item = source.ToArray()[i];
foreach (PropertyInfo pro in item.GetType().GetProperties()) { foreach(PropertyInfo pro in item.GetType().GetProperties()) {
if (!dt.Columns.Contains(pro.Name)) { if(!dt.Columns.Contains(pro.Name)) {
dt.Columns.Add(new DataColumn { dt.Columns.Add(new DataColumn {
ColumnName = pro.Name, ColumnName = pro.Name,
DataType = pro.PropertyType, DataType = pro.PropertyType,
@ -34,16 +34,15 @@ namespace Falcon.SugarApi
} }
} }
} }
foreach (var i in source) { foreach(var i in source) {
var row = dt.NewRow(); var row = dt.NewRow();
foreach (var p in i.GetType().GetProperties()) { foreach(var p in i.GetType().GetProperties()) {
if (p.CanRead) { if(p.CanRead) {
var val = p.GetValue(i); var val = p.GetValue(i);
try { try {
row[p.Name] = val; row[p.Name] = val;
} } catch(Exception ex) {
catch (Exception ex) { throw new Exception($"值设置失败!{p.Name}:{val}",ex);
throw new Exception($"值设置失败!{p.Name}:{val}", ex);
} }
} }
} }
@ -64,18 +63,18 @@ namespace Falcon.SugarApi
var type = typeof(T); var type = typeof(T);
var dt = new DataTable(); var dt = new DataTable();
var pros = typeof(T).GetProperties(); var pros = typeof(T).GetProperties();
foreach (PropertyInfo p in pros) { foreach(PropertyInfo p in pros) {
if (p.CanRead) { if(p.CanRead) {
dt.Columns.Add(new DataColumn { dt.Columns.Add(new DataColumn {
ColumnName = p.Name, ColumnName = p.Name,
DataType = p.PropertyType, DataType = p.PropertyType,
}); });
} }
} }
foreach (var i in source) { foreach(var i in source) {
var row = dt.NewRow(); var row = dt.NewRow();
foreach (var p in pros) { foreach(var p in pros) {
if (p.CanRead) { if(p.CanRead) {
var val = p.GetValue(i); var val = p.GetValue(i);
row[p.Name] = val; row[p.Name] = val;
} }
@ -94,13 +93,33 @@ namespace Falcon.SugarApi
/// <param name="reduceFunc">缩减方法</param> /// <param name="reduceFunc">缩减方法</param>
/// <param name="initialValue">缩减初始值</param> /// <param name="initialValue">缩减初始值</param>
/// <returns>缩减结果</returns> /// <returns>缩减结果</returns>
public static TR Reduce<T, TR>(this IEnumerable<T> source, Func<TR, T, TR> reduceFunc, TR initialValue) { public static TR Reduce<T, TR>(this IEnumerable<T> source,Func<TR,T,TR> reduceFunc,TR initialValue) {
if (reduceFunc == null) { if(reduceFunc == null) {
throw new ArgumentNullException(nameof(reduceFunc)); throw new ArgumentNullException(nameof(reduceFunc));
} }
var result = initialValue; var result = initialValue;
foreach (var i in source) { foreach(var i in source) {
result = reduceFunc(result, i); result = reduceFunc(result,i);
}
return result;
}
/// <summary>
/// 对枚举进行缩减,并返回缩减后的结果。
/// </summary>
/// <typeparam name="T">枚举的类型</typeparam>
/// <typeparam name="TR">缩减结果类型</typeparam>
/// <param name="source">原枚举</param>
/// <param name="reduceFunc">缩减方法</param>
/// <param name="initialValue">缩减初始值</param>
/// <returns>缩减结果</returns>
public static TR Reduce<T, TR>(this IEnumerable<T> source,TR initialValue,Func<TR,T,TR> reduceFunc) {
if(reduceFunc == null) {
throw new ArgumentNullException(nameof(reduceFunc));
}
var result = initialValue;
foreach(var i in source) {
result = reduceFunc(result,i);
} }
return result; return result;
} }