Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public interface IRangeValue<T> where T : struct
- {
- Nullable<T> High{get;set;}
- Nullable<T> Low { get; set; }
- }
- public class DataRangeT<T> : IRangeValue<T>
- where T : struct
- {
- Nullable<T> _high;
- Nullable<T> _low;
- public Nullable<T> High { get { return _high; } set { _high = value; } }
- public Nullable<T> Low { get { return _low; } set { _low = value; } }
- }
- public static class ExpressionHelperT<K>
- where K : struct
- {
- public static Expression<Func<T, bool>> RangeCompare<T>(Expression<Func<T, K>> selector, IRangeValue<K> patten)
- {
- Expression<Func<T, bool>> predicate = PredicateBuilder.True<T>();
- if (patten.High.HasValue)
- {
- predicate = predicate.And<T>(Expression.Lambda<Func<T, bool>>(Expression.LessThanOrEqual(selector, Expression.Constant(patten.High.Value, typeof(K)))));
- }
- if (patten.Low.HasValue)
- {
- predicate = predicate.And<T>(Expression.Lambda<Func<T, bool>>(Expression.GreaterThanOrEqual(selector, Expression.Constant(patten.Low.Value, typeof(K)))));
- }
- return predicate;
- }
- }
- public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> expr1,Expression<Func<T, bool>> expr2)
- {
- var invokedExpr = Expression.Invoke(expr2, expr1.Parameters.Cast<Expression>());
- return Expression.Lambda<Func<T, bool>>(Expression.AndAlso(expr1.Body, invokedExpr), expr1.Parameters);
- }
- class Program
- {
- static void Main(string[] args)
- {
- DataRangeT<DateTime> dataRange = new DataRangeT<DateTime>();
- dataRange.High = null;
- dataRange.Low = DateTime.Today;
- List<DateTime> dates = new List<DateTime>();
- dates.Add(new DateTime(2018, 1, 1));
- dates.Add(new DateTime(2018, 2, 1));
- dates.Add(new DateTime(2018, 3, 1));
- dates.Add(new DateTime(2018, 4, 1));
- dates.Add(new DateTime(2018, 5, 1));
- dates.Add(new DateTime(2018, 6, 1));
- dates.Add(new DateTime(2018, 7, 1));
- dates.Add(new DateTime(2018, 8, 1));
- dates.Add(new DateTime(2018, 9, 1));
- List<DateTime> results = dates.Where<DateTime>(ExpressionHelperT<DateTime>.RangeCompare<DateTime>(c => c.Date, dataRange)).ToList();
- foreach (DateTime dt in results)
- {
- Console.WriteLine(dt.ToShortDateString());
- }
- Console.ReadLine();
- }
- }
- Can't convert System.Collections.Generic.List<System.DateTime>”to“System.Linq.ParallelQuery<System.DateTime>”
- Can't convert“System.Linq.Expressions.Expression<System.Func<System.DateTime,bool>>”to“System.Func<System.DateTime,int,bool>”
Add Comment
Please, Sign In to add comment