/// <summary> /// 执行数据的过滤,输入待查询的属性列表 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="obj"></param> /// <param name="compare"></param> /// <param name="propertyNames"></param> /// <param name="searchText"></param> /// <param name="searchRegex"></param> /// <returns></returns> public static bool Filter <T>(T obj, ComparerEnum compare, string[] propertyNames, string searchText, Regex searchRegex) { Func <T, bool> fun = compare == ComparerEnum.StringContains ? CreateFilterDelegate(obj, propertyNames, compare, searchText) : CreateFilterDelegate(obj, propertyNames, compare, searchRegex); return(fun == null ? false : fun(obj)); }
public static IComparer <T> Create(ComparerEnum type) { if (type == ComparerEnum.Id) { return(new EmployeeCompareWithID() as IComparer <T>); } if (type == ComparerEnum.FirstName) { return(new EmployeeCompareWithFirstName() as IComparer <T>); } if (type == ComparerEnum.LastName) { return(new EmployeeCompareWithLastName() as IComparer <T>); } throw new InvalidOperationException(); }
/// <summary> /// 创建属性过滤委托,将会生成如p=>StringContains(p.Name,"12")||StringContains(p.Age.ToString(),"12")这样的lamda表达式 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="source"></param> /// <param name="propertyNames"></param> /// <param name="comparer"></param> /// <param name="searchPattern"></param> /// <returns></returns> private static Func <T, bool> CreateFilterDelegate <T>(T source, string[] propertyNames, ComparerEnum comparer, object searchPattern) { if (source == null) { return(null); } var realType = source.GetType(); //元素的实际类型,比如List<object> source时,返回Message if (DicFilterDelegate.ContainsKey(realType)) //如果保存了之前的lambda表达式 { return((Func <T, bool>)DicFilterDelegate[realType]); } //没有保存则直接创建 var elementtype = typeof(T); //元素类型,比如List<object> source时,返回object LambdaExpression lmbada = null; var parameter = Expression.Parameter(elementtype, "p"); Expression comparisionExpression = null; var constExpression = Expression.Constant(searchPattern); for (int i = 0; i < propertyNames.Length; i++) { string propertyName = propertyNames[i]; var property = realType.GetProperty(propertyName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase); if (property == null) { return(null); } Expression propertyAccess = realType != elementtype ? Expression.MakeMemberAccess(Expression.TypeAs(parameter, realType), property) : Expression.MakeMemberAccess(parameter, property); //如果实际类型和参数类型不一样,需要使用as转换 if (property.PropertyType.IsGenericType && property.PropertyType.GetGenericTypeDefinition() == typeof(Nullable <>)) { var getValueOrDefault = property.PropertyType.GetMethods().First(p => p.Name == "GetValueOrDefault"); propertyAccess = Expression.Call(propertyAccess, getValueOrDefault); } Expression comparisionExpression2; switch (comparer) { case ComparerEnum.StringContains: if (property.PropertyType != typeof(string)) { //如果不是string类型,则调用tostring方法 MethodInfo miToString; if (DicToStringMethod.ContainsKey(property.PropertyType)) { miToString = DicToStringMethod[property.PropertyType]; } else { miToString = property.PropertyType.GetMethod("ToString", new Type[0]); DicToStringMethod[property.PropertyType] = miToString; } //生成p.ToString() Expression expressionToString = Expression.Call(propertyAccess, miToString); comparisionExpression2 = Expression.Call(DicMethod["StringContains"], expressionToString, constExpression); } else { comparisionExpression2 = Expression.Call(DicMethod["StringContains"], propertyAccess, constExpression); } break; case ComparerEnum.Regex: if (property.PropertyType != typeof(string)) { MethodInfo miToString; if (DicToStringMethod.ContainsKey(property.PropertyType)) { miToString = DicToStringMethod[property.PropertyType]; } else { miToString = property.PropertyType.GetMethod("ToString", new Type[0]); DicToStringMethod[property.PropertyType] = miToString; } //生成p.ToString() Expression expressionToString = Expression.Call(propertyAccess, miToString); comparisionExpression2 = Expression.Call(DicMethod["StringRegex"], expressionToString, constExpression); } else { comparisionExpression2 = Expression.Call(DicMethod["StringRegex"], propertyAccess, constExpression); } break; default: comparisionExpression2 = Expression.Equal(propertyAccess, constExpression); break; } //如果有多个属性,则添加使用"||"连接多个条件 comparisionExpression = comparisionExpression == null ? comparisionExpression2 : Expression.OrElse(comparisionExpression, comparisionExpression2); } lmbada = Expression.Lambda(comparisionExpression, parameter); DicFilterDelegate[realType] = lmbada.Compile(); //最终将生成p=>StringContains(p.Name,"12")||StringContains(p.Age.ToString(),"12")这样的lamda表达式 return((Func <T, bool>)DicFilterDelegate[realType]); }
/// <summary> /// 执行数据的过滤,包含了全部属性计算 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="obj"></param> /// <param name="compare"></param> /// <param name="searchText"></param> /// <param name="searchRegex"></param> /// <returns></returns> public static bool Filter <T>(T obj, ComparerEnum compare, string searchText, Regex searchRegex) { string[] propertyNames = GetProperties(obj, null); return(Filter(obj, compare, propertyNames, searchText, searchRegex)); }
private static TSource ComparerBy <TSource, TKey>(this IEnumerable <TSource> source, Func <TSource, TKey> selector, IComparer <TKey> comparer, ComparerEnum comparerEnum) { if (source == null) { throw new ArgumentNullException("source"); } if (selector == null) { throw new ArgumentNullException("selector"); } if (comparer == null) { comparer = Comparer <TKey> .Default; } using (var sourceIterator = source.GetEnumerator()) { if (!sourceIterator.MoveNext()) { throw new InvalidOperationException("Sequence contains no elements"); } var value = sourceIterator.Current; var valueKey = selector(value); while (sourceIterator.MoveNext()) { var candidate = sourceIterator.Current; var candidateProjected = selector(candidate); if ((comparerEnum == ComparerEnum.Minor && comparer.Compare(candidateProjected, valueKey) < 0) || (comparerEnum == ComparerEnum.Major && comparer.Compare(candidateProjected, valueKey) > 0)) { value = candidate; valueKey = candidateProjected; } } return(value); } }
public IEnumerable <T> Sort(IEnumerable <T> array, SortingMethodEnum sortingMethod, ComparerEnum comparerEnum) { IComparer <T> comparer = ComparerFactory <T> .Create(comparerEnum); switch (sortingMethod) { case SortingMethodEnum.BubbleSort: _sortingMethod = new BubbleSort <T>(); break; case SortingMethodEnum.QuickSort: _sortingMethod = new QuickShort <T>(); break; } return(_sortingMethod.Sort(array, comparer)); }