jeudi 28 juin 2018

filter database columns by reflection with lambda expressions in c#

I am using datatable with individual filtering(each column contains a search box in the table header). I want to use lambda expression to filter the datatable but using the reflection way.I have the column name of the datatabase as a string, and the search value too.I may send one or more column names with their values( send the column name as general parameter with the search value, and the method should know how to classify the column name by reflection method):

 Expression<Func<DatatabseView, bool>> filter = x => x.ColumnName.Contains(SearchValue);

And this is my repository method:

    /// <summary>
    /// Get set of object depends on condition, ordereds by order expression, with pageing and joined with include properties.
    /// </summary>
    /// <param name="filter">Condition to be filter data store by.</param>
    /// <param name="orderBy">Order by Expression.</param>
    /// <param name="pageNum">Page number to used at pageing.</param>
    /// <param name="pageSize">Page size to used at pageing.</param>
    /// <param name="disableTracking">disable tracking</param>
    /// <param name="includeProperties">including related entities</param>
    /// <returns>Ienumerable entities</returns>
    public async Task<IEnumerable<T>> Get(Expression<Func<T, bool>> filter = null,
        Func<IQueryable<T>, IOrderedQueryable<T>> orderBy = null,
        int? pageNum = null,
        int? pageSize = null,
        bool disableTracking = true,
        Func<IQueryable<T>, IIncludableQueryable<T, object>> includeProperties = null)
    {
        IQueryable<T> query = DbSet;
        if (disableTracking)
            query = query.AsNoTracking();
        if (includeProperties != null)
            query = includeProperties(query);
        if (filter != null)
            query = query.Where(filter);
        if (orderBy != null)
            query = orderBy(query);
        if (pageNum != null && pageSize != null)
            query = query.Skip(pageNum.Value ).Take(pageSize.Value);
        return await query.ToListAsync();
    }

How to do this kind of search using lambda expressions with reflection?





Aucun commentaire:

Enregistrer un commentaire