Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Expression<Func<Client, bool>> expression = x => true;
- foreach (var item in searchParams)
- {
- var operatorType = ExpressionType.Equal;
- string propertyName = null;
- object value = null;
- string keyValue = item.Value;
- if (item.Key == Constants.SearchParameterNames.Id)
- {
- int val = 0;
- if (int.TryParse(keyValue, out val))
- value = val;
- propertyName = "ClientID";
- }
- else if (item.Key == Constants.SearchParameterNames.Lastupdate)
- {
- DateTime dateTime;
- if (DateTime.TryParse(keyValue, out dateTime))
- value = dateTime;
- propertyName = "LastChange";
- }
- if (!string.IsNullOrWhiteSpace(propertyName) && value != null)
- {
- var exp = GetBinaryOperation<Client>(propertyName, operatorType, value);
- var exp1 = Expression.And(expression.Body, exp);
- expression = Expression.Lambda<Func<Client, bool>>(exp1, expression.Parameters);
- }
- }
- var client = _clientRepository.FindBy(expression).ToList();
- public BinaryExpression GetBinaryOperation<T>(string propertyName, ExpressionType type, object value)
- {
- var parameterExpression = Expression.Parameter(typeof(T), "x");
- var memberExpression = Expression.Property(parameterExpression, propertyName);
- var propertyType = GetMemberType(memberExpression);
- var rhs = Expression.Constant(value);
- var binaryExpression = Expression.MakeBinary(type, memberExpression, rhs);
- return binaryExpression;
- }
Add Comment
Please, Sign In to add comment