Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to select multi columns filter with lambda expression

Tags:

c#

lambda

is there a short way, I don't want to use "if state" for every situation what do you think Description : type of query is IQueryable

public class OrderFilter{
     public string SearchValue { get => _searchValue.ToLower(); set => _searchValue = value; }
     public string[] SearchColumns { get; set; }
}
if (!string.IsNullOrWhiteSpace(orderFilter.SearchValue))
            
   if (orderFilter.SearchColumns.Contains("warehouse"))
       query = query.Where(x => x.Warehouse.Description.Contains(orderFilter.SearchValue));
   if (orderFilter.SearchColumns.Contains("date"))
      query = query.Where(x => x.Date.Contains(orderFilter.SearchValue));
   if (orderFilter.SearchColumns.Contains("warehouse") && orderFilter.SearchColumns.Contains("date"))
      query = query.Where(x => x.OrderNo.Contains(orderFilter.SearchValue)
                    || x.Warehouse.Description.Contains(orderFilter.SearchValue)
                    || x.Client.Description.Contains(orderFilter.SearchValue)
                    || x.Date.ToString().Contains(orderFilter.SearchValue)
                    || x.Salesman.Username.Contains(orderFilter.SearchValue)
                    );
            } 

enter image description here

like image 501
Cll Avatar asked Nov 18 '25 05:11

Cll


1 Answers

Access nested properties with dynamic lambda using Linq.Expression found the answer I was looking for

public class LambdaHelper
    {
        public Expression<Func<TSource, bool>> MultiSearchOrder<TSource>(string[] columns, string value)
        {
            ParameterExpression parameter = Expression.Parameter(typeof(TSource), "x"); // x=>  . ... demektir. parametre
            MethodInfo containsMethod = typeof(String).GetMethod("Contains", new Type[] { typeof(String) });
            Expression dynamiclambda = null;
            MethodCallExpression call = null;
            foreach (var propertyName in columns)
            {

                MemberExpression propertyAccess = NestedExpressionProperty(parameter, propertyName);
                call = Expression.Call(propertyAccess, containsMethod, Expression.Constant(value));
                if (null == dynamiclambda)
                {
                    dynamiclambda = call;
                }
                else
                {
                    dynamiclambda = Expression.Or(dynamiclambda, call);
                }
            }
            Expression<Func<TSource, bool>> predicate = Expression.Lambda<Func<TSource, bool>>(dynamiclambda, parameter);

            return predicate;

        }
 
        private MemberExpression NestedExpressionProperty(Expression expression, string propertyName)
        {
            string[] parts = propertyName.Split('.');
            int partsL = parts.Length;

            return (partsL > 1)
                ?
                Expression.Property(
                    NestedExpressionProperty(
                        expression,
                        parts.Take(partsL - 1)
                            .Aggregate((a, i) => a + "." + i)
                    ),
                    parts[partsL - 1])
                :
                Expression.Property(expression, propertyName);
        }
like image 94
Cll Avatar answered Nov 19 '25 20:11

Cll



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!