L
L
lucky42021-04-14 11:40:22
ASP.NET
lucky4, 2021-04-14 11:40:22

How to pass values ​​to url?

There is a generic function for filters:

private static Expression<Func<T, bool>> GetColumnEquality<T>(string property, string term)
{
    var obj = Expression.Parameter(typeof(T), "obj");        

    var objProperty = Expression.PropertyOrField(obj, property);
    var objEquality = Expression.Equal(objProperty, Expression.Constant(term));

    var lambda = Expression.Lambda<Func<T, bool>>(objEquality, obj);

    return lambda;
}

public static IQueryable<T> Filter<T>(IQueryable<T> source, string searchTerm)
{
    var propNames = typeof(T).GetProperties(BindingFlags.Instance | BindingFlags.Public)
                             .Where(e => e.PropertyType == typeof(string))
                             .Select(x => x.Name).ToList();

    var predicate = PredicateBuilder.False<T>();

    foreach(var name in propNames)
    {
        predicate = predicate.Or(GetColumnEquality<T>(name, searchTerm));
    }

    return source.Where(predicate);
}


Then I want to call this function from the service: How can I pass here the property I want and its value? And how to pass it as a query ? For example, if it looks like this:ApplyFiltering("")

public class Foo
{
    public string Bar { get; set; }
    public string Qux { get; set; }
}

Filter<Foo>(Enumerable.Empty<Foo>().AsQueryable(), "Hello");

// Expression Generated by Predicate Builder
// f => ((False OrElse Invoke(obj => (obj.Bar == "Hello"), f)) OrElse Invoke(obj => (obj.Qux == "Hello"), f))

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question