QueryHelper.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. using Ant.Query;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Linq.Expressions;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace Ant.ORM.Query
  9. {
  10. static class QueryHelper
  11. {
  12. public static Expression<TDelegate> ComposePredicate<TDelegate>(List<LambdaExpression> filterPredicates, Expression[] expressionSubstitutes, ParameterExpression parameter)
  13. {
  14. Expression predicateBody = null;
  15. foreach (LambdaExpression filterPredicate in filterPredicates)
  16. {
  17. var body = JoinQueryParameterExpressionReplacer.Replace(filterPredicate, expressionSubstitutes, parameter).Body;
  18. if (predicateBody == null)
  19. {
  20. predicateBody = body;
  21. }
  22. else
  23. {
  24. predicateBody = Expression.AndAlso(predicateBody, body);
  25. }
  26. }
  27. Expression<TDelegate> predicate = Expression.Lambda<TDelegate>(predicateBody, parameter);
  28. return predicate;
  29. }
  30. public static Expression[] MakeExpressionSubstitutes(Type joinResultType, ParameterExpression parameter)
  31. {
  32. int joinResultTypeGenericArgumentCount = joinResultType.GetGenericArguments().Length;
  33. Expression[] expressionSubstitutes = new Expression[joinResultTypeGenericArgumentCount];
  34. for (int i = 0; i < joinResultTypeGenericArgumentCount; i++)
  35. {
  36. expressionSubstitutes[i] = Expression.MakeMemberAccess(parameter, joinResultType.GetProperty("Item" + (i + 1).ToString()));
  37. }
  38. return expressionSubstitutes;
  39. }
  40. }
  41. }