枚举类型增加了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);
 | 
			
		||||
            //测试列
 | 
			
		||||
            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<ToDataTableTestModel> {
 | 
			
		||||
@ -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<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 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 },
 | 
			
		||||
@ -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<person>(),(a,b) => {
 | 
			
		||||
            men=people.Reduce(new List<person>(),(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<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.Collections.Generic;
 | 
			
		||||
using System.Data;
 | 
			
		||||
using System.Diagnostics.CodeAnalysis;
 | 
			
		||||
using System.Linq;
 | 
			
		||||
using System.Reflection;
 | 
			
		||||
 | 
			
		||||
@ -18,18 +19,18 @@ namespace Falcon.SugarApi
 | 
			
		||||
        /// <returns>转换后Datatable</returns>
 | 
			
		||||
        /// <exception cref="ArgumentNullException">参数为Null</exception>
 | 
			
		||||
        public static DataTable ToDataTable(this IEnumerable<object> 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<source.Count();i++) {
 | 
			
		||||
                var item = source.ToArray()[i];
 | 
			
		||||
                foreach(PropertyInfo pro in item.GetType().GetProperties()) {
 | 
			
		||||
                    if(!dt.Columns.Contains(pro.Name)) {
 | 
			
		||||
                        dt.Columns.Add(new DataColumn {
 | 
			
		||||
                            ColumnName = pro.Name,
 | 
			
		||||
                            DataType = pro.PropertyType,
 | 
			
		||||
                            ColumnName=pro.Name,
 | 
			
		||||
                            DataType=pro.PropertyType,
 | 
			
		||||
                        });
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
@ -40,8 +41,9 @@ namespace Falcon.SugarApi
 | 
			
		||||
                    if(p.CanRead) {
 | 
			
		||||
                        var val = p.GetValue(i);
 | 
			
		||||
                        try {
 | 
			
		||||
                            row[p.Name] = val;
 | 
			
		||||
                        } catch(Exception ex) {
 | 
			
		||||
                            row[p.Name]=val;
 | 
			
		||||
                        }
 | 
			
		||||
                        catch(Exception ex) {
 | 
			
		||||
                            throw new Exception($"值设置失败!{p.Name}:{val}",ex);
 | 
			
		||||
                        }
 | 
			
		||||
                    }
 | 
			
		||||
@ -59,15 +61,15 @@ namespace Falcon.SugarApi
 | 
			
		||||
        /// <returns>转换后Datatable</returns>
 | 
			
		||||
        /// <exception cref="ArgumentNullException">参数为Null</exception>
 | 
			
		||||
        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 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
 | 
			
		||||
        /// <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) {
 | 
			
		||||
            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
 | 
			
		||||
        /// <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) {
 | 
			
		||||
            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;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        /// <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…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user