枚举类型增加了IsNullOrEmpty方法,判断枚举是否为null或者没有任何元素,并进行测试。
This commit is contained in:
parent
128f5f6025
commit
f5a44f9569
|
@ -28,23 +28,23 @@ namespace Falcon.SugarApi.Test
|
||||||
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");
|
||||||
Assert.IsTrue(col1.DataType == typeof(int));
|
Assert.IsTrue(col1.DataType==typeof(int));
|
||||||
var col2 = r.Columns[1];
|
var col2 = r.Columns[1];
|
||||||
Assert.IsTrue(col2.ColumnName == "b");
|
Assert.IsTrue(col2.ColumnName=="b");
|
||||||
Assert.IsTrue(col2.DataType == typeof(string));
|
Assert.IsTrue(col2.DataType==typeof(string));
|
||||||
var col3 = r.Columns[2];
|
var col3 = r.Columns[2];
|
||||||
Assert.IsTrue(col3.ColumnName == "c");
|
Assert.IsTrue(col3.ColumnName=="c");
|
||||||
Assert.IsTrue(col3.DataType == typeof(int));
|
Assert.IsTrue(col3.DataType==typeof(int));
|
||||||
//测试行
|
//测试行
|
||||||
var row = r.Rows[0];
|
var row = r.Rows[0];
|
||||||
Assert.IsTrue(row[0].ToString() == "1");
|
Assert.IsTrue(row[0].ToString()=="1");
|
||||||
Assert.IsTrue(row[1].ToString() == "b1");
|
Assert.IsTrue(row[1].ToString()=="b1");
|
||||||
Assert.IsTrue(row[2].ToString() == "");
|
Assert.IsTrue(row[2].ToString()=="");
|
||||||
row = r.Rows[1];
|
row=r.Rows[1];
|
||||||
Assert.IsTrue((int)row[0] == 2);
|
Assert.IsTrue((int)row[0]==2);
|
||||||
Assert.IsTrue((string)row[1] == "b2");
|
Assert.IsTrue((string)row[1]=="b2");
|
||||||
Assert.IsTrue((int)row[2] == 1);
|
Assert.IsTrue((int)row[2]==1);
|
||||||
Console.WriteLine($"ToDataTable测试完成");
|
Console.WriteLine($"ToDataTable测试完成");
|
||||||
|
|
||||||
var models = new List<ToDataTableTestModel> {
|
var models = new List<ToDataTableTestModel> {
|
||||||
|
@ -52,7 +52,7 @@ namespace Falcon.SugarApi.Test
|
||||||
new ToDataTableTestModel{id=2,Name="name2" },
|
new ToDataTableTestModel{id=2,Name="name2" },
|
||||||
};
|
};
|
||||||
sw.Reset();
|
sw.Reset();
|
||||||
r = models.ToDataTable();
|
r=models.ToDataTable();
|
||||||
sw.Stop();
|
sw.Stop();
|
||||||
Console.WriteLine($"ToDataTable<>转换,共用时{sw.ElapsedMilliseconds}毫秒");
|
Console.WriteLine($"ToDataTable<>转换,共用时{sw.ElapsedMilliseconds}毫秒");
|
||||||
//测试表
|
//测试表
|
||||||
|
@ -60,19 +60,19 @@ namespace Falcon.SugarApi.Test
|
||||||
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");
|
||||||
Assert.IsTrue(col1.DataType == typeof(int));
|
Assert.IsTrue(col1.DataType==typeof(int));
|
||||||
col2 = r.Columns[1];
|
col2=r.Columns[1];
|
||||||
Assert.IsTrue(col2.ColumnName == "Name");
|
Assert.IsTrue(col2.ColumnName=="Name");
|
||||||
Assert.IsTrue(col2.DataType == typeof(string));
|
Assert.IsTrue(col2.DataType==typeof(string));
|
||||||
//测试行
|
//测试行
|
||||||
row = r.Rows[0];
|
row=r.Rows[0];
|
||||||
Assert.IsTrue(row[0].ToString() == "1");
|
Assert.IsTrue(row[0].ToString()=="1");
|
||||||
Assert.IsTrue(row[1].ToString() == "");
|
Assert.IsTrue(row[1].ToString()=="");
|
||||||
row = r.Rows[1];
|
row=r.Rows[1];
|
||||||
Assert.IsTrue((int)row[0] == 2);
|
Assert.IsTrue((int)row[0]==2);
|
||||||
Assert.IsTrue(row[1].ToString() == "name2");
|
Assert.IsTrue(row[1].ToString()=="name2");
|
||||||
Console.WriteLine($"ToDataTable<>测试完成");
|
Console.WriteLine($"ToDataTable<>测试完成");
|
||||||
|
|
||||||
|
|
||||||
|
@ -81,10 +81,10 @@ 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);
|
sum=list.Reduce(0,(a,b) => a+b);
|
||||||
Assert.IsTrue(sum == 10,"对数组求和错误");
|
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 },
|
||||||
|
@ -92,10 +92,10 @@ 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);
|
sumage=people.Reduce(0,(a,b) => b.IsMan ? a+b.age : a);
|
||||||
Assert.IsTrue(sumage == 30 + 50 + 60,"有条件求和错误");
|
Assert.IsTrue(sumage==30+50+60,"有条件求和错误");
|
||||||
|
|
||||||
var men = people.Reduce((a,b) => {
|
var men = people.Reduce((a,b) => {
|
||||||
if(b.IsMan) {
|
if(b.IsMan) {
|
||||||
|
@ -106,7 +106,7 @@ namespace Falcon.SugarApi.Test
|
||||||
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) => {
|
men=people.Reduce(new List<person>(),(a,b) => {
|
||||||
if(b.IsMan) {
|
if(b.IsMan) {
|
||||||
a.Add(b);
|
a.Add(b);
|
||||||
}
|
}
|
||||||
|
@ -118,7 +118,20 @@ namespace Falcon.SugarApi.Test
|
||||||
|
|
||||||
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","空集合返回初始值,并且不调用缩减方法。");
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod("IsNullOrEmpty测试")]
|
||||||
|
public void IsNullOrEmptyTest() {
|
||||||
|
List<int> list = null;
|
||||||
|
Assert.IsTrue(list.IsNullOrEmpty());
|
||||||
|
Assert.IsFalse(list.IsNotNullOrEmpty());
|
||||||
|
list=new List<int>();
|
||||||
|
Assert.IsTrue(list.IsNullOrEmpty());
|
||||||
|
Assert.IsFalse(list.IsNotNullOrEmpty());
|
||||||
|
list.Add(1);
|
||||||
|
Assert.IsFalse(list.IsNullOrEmpty());
|
||||||
|
Assert.IsTrue(list.IsNotNullOrEmpty());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Data;
|
using System.Data;
|
||||||
|
using System.Diagnostics.CodeAnalysis;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
|
|
||||||
|
@ -18,18 +19,18 @@ namespace Falcon.SugarApi
|
||||||
/// <returns>转换后Datatable</returns>
|
/// <returns>转换后Datatable</returns>
|
||||||
/// <exception cref="ArgumentNullException">参数为Null</exception>
|
/// <exception cref="ArgumentNullException">参数为Null</exception>
|
||||||
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,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -40,8 +41,9 @@ namespace Falcon.SugarApi
|
||||||
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -59,15 +61,15 @@ namespace Falcon.SugarApi
|
||||||
/// <returns>转换后Datatable</returns>
|
/// <returns>转换后Datatable</returns>
|
||||||
/// <exception cref="ArgumentNullException">参数为Null</exception>
|
/// <exception cref="ArgumentNullException">参数为Null</exception>
|
||||||
public static DataTable ToDataTable<T>(this IEnumerable<T> source) {
|
public static DataTable ToDataTable<T>(this IEnumerable<T> source) {
|
||||||
_ = source ?? throw new ArgumentNullException(nameof(source));
|
_=source??throw new ArgumentNullException(nameof(source));
|
||||||
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,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -76,7 +78,7 @@ namespace Falcon.SugarApi
|
||||||
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
dt.Rows.Add(row);
|
dt.Rows.Add(row);
|
||||||
|
@ -94,12 +96,12 @@ namespace Falcon.SugarApi
|
||||||
/// <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;
|
return result;
|
||||||
}
|
}
|
||||||
|
@ -114,14 +116,33 @@ namespace Falcon.SugarApi
|
||||||
/// <param name="initialValue">缩减初始值</param>
|
/// <param name="initialValue">缩减初始值</param>
|
||||||
/// <returns>缩减结果</returns>
|
/// <returns>缩减结果</returns>
|
||||||
public static TR Reduce<T, TR>(this IEnumerable<T> source,TR initialValue,Func<TR,T,TR> reduceFunc) {
|
public static TR Reduce<T, TR>(this IEnumerable<T> source,TR initialValue,Func<TR,T,TR> reduceFunc) {
|
||||||
if(reduceFunc == null) {
|
if(reduceFunc==null) {
|
||||||
throw new ArgumentNullException(nameof(reduceFunc));
|
throw new ArgumentNullException(nameof(reduceFunc));
|
||||||
}
|
}
|
||||||
var result = initialValue;
|
var result = initialValue;
|
||||||
|
if(source.IsNull()) {
|
||||||
|
|
||||||
|
}
|
||||||
foreach(var i in source) {
|
foreach(var i in source) {
|
||||||
result = reduceFunc(result,i);
|
result=reduceFunc(result,i);
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 返回枚举是否为null或者集合无元素
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">枚举的元素类型</typeparam>
|
||||||
|
/// <param name="values">枚举对象</param>
|
||||||
|
/// <returns>为null或者无元素返回True,否则False</returns>
|
||||||
|
public static bool IsNullOrEmpty<T>([AllowNull] this IEnumerable<T> values) => values==null||values.Count()==0;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 返回枚举是否不为null或者集合无元素,结果是对IsNullOrEmpty去反。
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">枚举的元素类型</typeparam>
|
||||||
|
/// <param name="values">枚举对象</param>
|
||||||
|
/// <returns>枚举不为null或者无元素返回True,否则False</returns>
|
||||||
|
public static bool IsNotNullOrEmpty<T>([AllowNull] this IEnumerable<T> values) => !values.IsNullOrEmpty();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user