c# - 动态 Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> 表达式

标签 c# iqueryable linq-expressions iorderedqueryable

我正在使用这里提到的模式 http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application

我正在使用下面的方法查询 EF

public virtual IEnumerable<TEntity> Get(
        Expression<Func<TEntity, bool>> filter = null,
        Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
        string includeProperties = "")
    {
        IQueryable<TEntity> query = dbSet;

        if (filter != null)
        {
            query = query.Where(filter);
        }

        foreach (var includeProperty in includeProperties.Split
            (new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
        {
            query = query.Include(includeProperty);
        }

        if (orderBy != null)
        {
            return orderBy(query).ToList();
        }
        else
        {
            return query.ToList();
        }
    }

现在我想创建动态 Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>>表达式来订购我的数据。

我只知道字段名称为字符串和订单类型(升序,降序)为字符串(asc,desc)

最佳答案

终于可以写出我想要的方法了。

 public static Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> GetOrderBy(string orderColumn, string orderType) {
            Type typeQueryable = typeof(IQueryable<TEntity>);
            ParameterExpression argQueryable = Expression.Parameter(typeQueryable, "p");
            var outerExpression = Expression.Lambda(argQueryable, argQueryable);
            string[] props = orderColumn.Split('.');
            IQueryable<TEntity> query = new List<TEntity>().AsQueryable<TEntity>();
            Type type = typeof(TEntity);
            ParameterExpression arg = Expression.Parameter(type, "x");

            Expression expr = arg;
            foreach(string prop in props) {
                PropertyInfo pi = type.GetProperty(prop, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance);
                expr = Expression.Property(expr, pi);
                type = pi.PropertyType;
            }
            LambdaExpression lambda = Expression.Lambda(expr, arg);
            string methodName = orderType == "asc" ? "OrderBy" : "OrderByDescending";

            MethodCallExpression resultExp =
                Expression.Call(typeof(Queryable), methodName, new Type[] { typeof(TEntity), type }, outerExpression.Body, Expression.Quote(lambda));
            var finalLambda = Expression.Lambda(resultExp, argQueryable);
            return (Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>>)finalLambda.Compile();
        }

此方法有两个参数,第一个是字段名,另一个是asc 或desc。 方法的结果可以直接与 IQueryable 对象一起使用。

谢谢你的帮助

关于c# - 动态 Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> 表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16009694/

相关文章:

c# - Integer 包含 linq c# 但使用 using 表达式

c# - 覆盖 machine.config 中的单个属性

c# - 如何动态构建与 Linq 和 Entity Framework 相关的 Select

c# - 尝试使用父属性作为子集合表达式中的参数; LinqKit 抛出 "Unable to cast MethodCallExpressionN to LambdaExpression"

c# - IQueryable参数——如何查看?

c# - 按特定顺序对 IQueryable 进行排序

c# - 在 .NET 3.5 中有效的语句抛出 "error CS0117: ' System.Linq.Dynamic.DynamicExpression' does not contain a definition for 'ParseLamba' "in 4.5

c# - 消息框快捷方式

c# - Ajax Accordion Pane : separate required field validators only triggered by a button in its pane

c# - 在 DataGrid 上编辑多个单元格然后保存到数据库