Guest User

Untitled

a guest
Dec 15th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.94 KB | None | 0 0
  1. Expression<Func<Client, bool>> expression = x => true;
  2. foreach (var item in searchParams)
  3. {
  4. var operatorType = ExpressionType.Equal;
  5. string propertyName = null;
  6. object value = null;
  7. string keyValue = item.Value;
  8.  
  9. if (item.Key == Constants.SearchParameterNames.Id)
  10. {
  11. int val = 0;
  12. if (int.TryParse(keyValue, out val))
  13. value = val;
  14. propertyName = "ClientID";
  15. }
  16. else if (item.Key == Constants.SearchParameterNames.Lastupdate)
  17. {
  18. DateTime dateTime;
  19. if (DateTime.TryParse(keyValue, out dateTime))
  20. value = dateTime;
  21. propertyName = "LastChange";
  22. }
  23.  
  24. if (!string.IsNullOrWhiteSpace(propertyName) && value != null)
  25. {
  26.  
  27. var exp = GetBinaryOperation<Client>(propertyName, operatorType, value);
  28. var exp1 = Expression.And(expression.Body, exp);
  29. expression = Expression.Lambda<Func<Client, bool>>(exp1, expression.Parameters);
  30. }
  31.  
  32. }
  33. var client = _clientRepository.FindBy(expression).ToList();
  34.  
  35. public BinaryExpression GetBinaryOperation<T>(string propertyName, ExpressionType type, object value)
  36. {
  37.  
  38. var parameterExpression = Expression.Parameter(typeof(T), "x");
  39. var memberExpression = Expression.Property(parameterExpression, propertyName);
  40. var propertyType = GetMemberType(memberExpression);
  41. var rhs = Expression.Constant(value);
  42. var binaryExpression = Expression.MakeBinary(type, memberExpression, rhs);
  43.  
  44. return binaryExpression;
  45. }
Add Comment
Please, Sign In to add comment