Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.62 KB | None | 0 0
  1. public static ObservableCollection<SI> GetDisplayStratsByJurisd(string jurisd, short Year,
  2. Expression<Func<string, bool>> lambStat)
  3. {
  4. var ctx = new MI_Entities(server, database);
  5.  
  6. var strats = from y in ctx.SIset.AsNoTracking()
  7. where y.Jurisd == jurisd && y.Year_ID == Year && lambStat(y.Status)
  8. select y;
  9. return new ObservableCollection<SI>(strats);
  10. }
  11.  
  12. public static ObservableCollection<SI> GetDisplayStratsByJurisd(string jurisd, short Year,
  13. Expression<Func<string, bool>> lambStat)
  14. {
  15. var ctx = new MI_Entities(server, database);
  16. Func<string, bool> bob = lambStat.Compile();
  17. var strats = from y in ctx.SIset.AsNoTracking()
  18. where y.Jurisd == jurisd && y.Year_ID == Year && bob(y.Status)
  19. select y;
  20. return new ObservableCollection<SI>(strats);
  21. }
  22.  
  23. // A function that takes two Expression<Func<TSource, bool>> and returns the AND expression
  24. static Expression<Func<TSource, bool>> AndAlso<TSource> (
  25. this Expression<Func<TSource, bool>> x,
  26. Expression<Func<TSource, bool>> y)
  27. {
  28. // TODO implement
  29. }
  30.  
  31. Expression<Func<Student, bool>> expr1 = student => student.City == "Birmingham";
  32. Expression<Func<Student, bool>> expr2 = student => student.Gender == Gender.Male;
  33. Expression<Func<Student, bool>> exprAND = expr1.AndAlso(expr2);
  34.  
  35. var brummyMaleStudents = dbContext.Students.Where(exprAnd).Select(...);
  36.  
  37. .Where(student => student.City == "Birmingham" && student.Gender == Gender.Male)
  38.  
  39. internal class ReplaceExpressionVisitor : ExpressionVisitor
  40. {
  41. private readonly Expression oldValue;
  42. private readonly Expression newValue;
  43.  
  44. public ReplaceExpressionVisitor(ParameterExpression oldValue,
  45. ParameterExpression newValue)
  46. {
  47. this.oldValue = oldValue;
  48. this.newValue = newValue;
  49. }
  50.  
  51. public override Expression Visit(Expression node)
  52. {
  53. if (node == this.oldValue)
  54. { // "my" expression is visited
  55. return this.newValue;
  56. }
  57. else
  58. { // not my Expression, I don't know how to Visit it, let the base class handle this
  59. return base.Visit(node);
  60. }
  61. }
  62. }
  63.  
  64. static Expression<Func<TSource, bool>> AndAlso<TSource>(
  65. this Expression<Func<TSource, bool>> expr1,
  66. Expression<Func<TSource, bool>> expr2)
  67. {
  68. // Create one expression that represent expr1 && expr2
  69. // the input of expr1 is a TSource,
  70. // the input of expr2 is a TSource,
  71. // so the input of expr1 && expr2 is a TSource:
  72. ParameterExpression inputParameter = Expression.Parameter(typeof(TSource));
  73.  
  74. // Visit the left part of the AND:
  75. var leftVisitor = new ReplaceExpressionVisitor(expr1.Parameters[0], inputParameter)
  76. var left = leftVisitor.Visit(expr1.Body);
  77.  
  78. // Visit the right part of the AND:
  79. var rightVisitor = new ReplaceExpressionVisitor(expr2.Parameters[0], parameter);
  80. var right = rightVisitor.Visit(expr2.Body);
  81.  
  82. // Combine left and right with a binary expression representing left && right:
  83. var andExpression = Expression.AndAlso(left, right);
  84.  
  85. // return the lambda expression that takes one Tsource as input and returns the AND:
  86. var lambda = Expression.Lambda<Func<TSource, bool>>(andExpression, new[] {parameter});
  87. return lambda;
  88. }
  89.  
  90. Expression<Func<Student, bool>> expr1 = student => student.City == "Birmingham";
  91. Expression<Func<Student, bool>> expr2 = student => student.Gender == Gender.Male;
  92.  
  93. var brummyMaleStudents = dbContext.Students.Where(expr1.AndAlso(expr2));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement