新增对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}毫秒");
//测试表
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<int> { 1, 2, 3, 4 };
var sum = list.Reduce((a, b) => a + b, 0);
Assert.IsTrue(sum == 10, "对数组求和错误");
var list = new List<int> { 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<person> {
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<person>());
foreach (var p in men) {
Assert.IsTrue(p.IsMan, "缩减为男性集合错误!");
},new List<person>());
foreach(var p in men) {
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 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","空集合返回初始值,并且不调用缩减方法。");
}
}

View File

@ -20,13 +20,13 @@ namespace Falcon.SugarApi
public static DataTable ToDataTable(this IEnumerable<object> 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
/// <param name="reduceFunc">缩减方法</param>
/// <param name="initialValue">缩减初始值</param>
/// <returns>缩减结果</returns>
public static TR Reduce<T, TR>(this IEnumerable<T> source, Func<TR, T, TR> reduceFunc, TR initialValue) {
if (reduceFunc == null) {
public static TR Reduce<T, TR>(this IEnumerable<T> source,Func<TR,T,TR> 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;
}
/// <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;
}