在C#

时间:2018-06-29 06:39:02

标签: c# linq reflection lambda

我正在使用具有单独过滤功能的数据表(每列在表标题中都包含一个搜索框)。我想使用lambda表达式来过滤数据表,但要使用反射方式。我将数据库的列名作为字符串以及搜索值。我可能会发送一个或多个列名及其值(发送列名作为具有搜索值的常规参数,该方法应该知道如何通过反射方法对列名称进行分类):

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

这是我的存储库方法:

    /// <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();
    }

如何使用带反射的lambda表达式进行这种搜索?

0 个答案:

没有答案