Guest User

Untitled

a guest
Jan 16th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. public interface IRangeValue<T> where T : struct
  2. {
  3. Nullable<T> High{get;set;}
  4. Nullable<T> Low { get; set; }
  5. }
  6.  
  7. public class DataRangeT<T> : IRangeValue<T>
  8. where T : struct
  9. {
  10. Nullable<T> _high;
  11. Nullable<T> _low;
  12. public Nullable<T> High { get { return _high; } set { _high = value; } }
  13. public Nullable<T> Low { get { return _low; } set { _low = value; } }
  14.  
  15. }
  16.  
  17. public static class ExpressionHelperT<K>
  18. where K : struct
  19. {
  20. public static Expression<Func<T, bool>> RangeCompare<T>(Expression<Func<T, K>> selector, IRangeValue<K> patten)
  21. {
  22. Expression<Func<T, bool>> predicate = PredicateBuilder.True<T>();
  23. if (patten.High.HasValue)
  24. {
  25. predicate = predicate.And<T>(Expression.Lambda<Func<T, bool>>(Expression.LessThanOrEqual(selector, Expression.Constant(patten.High.Value, typeof(K)))));
  26. }
  27. if (patten.Low.HasValue)
  28. {
  29. predicate = predicate.And<T>(Expression.Lambda<Func<T, bool>>(Expression.GreaterThanOrEqual(selector, Expression.Constant(patten.Low.Value, typeof(K)))));
  30. }
  31. return predicate;
  32. }
  33.  
  34. }
  35.  
  36. public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> expr1,Expression<Func<T, bool>> expr2)
  37. {
  38. var invokedExpr = Expression.Invoke(expr2, expr1.Parameters.Cast<Expression>());
  39. return Expression.Lambda<Func<T, bool>>(Expression.AndAlso(expr1.Body, invokedExpr), expr1.Parameters);
  40. }
  41.  
  42. class Program
  43. {
  44. static void Main(string[] args)
  45. {
  46. DataRangeT<DateTime> dataRange = new DataRangeT<DateTime>();
  47.  
  48. dataRange.High = null;
  49. dataRange.Low = DateTime.Today;
  50.  
  51. List<DateTime> dates = new List<DateTime>();
  52. dates.Add(new DateTime(2018, 1, 1));
  53. dates.Add(new DateTime(2018, 2, 1));
  54. dates.Add(new DateTime(2018, 3, 1));
  55. dates.Add(new DateTime(2018, 4, 1));
  56. dates.Add(new DateTime(2018, 5, 1));
  57. dates.Add(new DateTime(2018, 6, 1));
  58. dates.Add(new DateTime(2018, 7, 1));
  59. dates.Add(new DateTime(2018, 8, 1));
  60. dates.Add(new DateTime(2018, 9, 1));
  61.  
  62. List<DateTime> results = dates.Where<DateTime>(ExpressionHelperT<DateTime>.RangeCompare<DateTime>(c => c.Date, dataRange)).ToList();
  63.  
  64. foreach (DateTime dt in results)
  65. {
  66. Console.WriteLine(dt.ToShortDateString());
  67. }
  68. Console.ReadLine();
  69. }
  70. }
  71.  
  72. Can't convert System.Collections.Generic.List<System.DateTime>”to“System.Linq.ParallelQuery<System.DateTime>”
  73. 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