【问题标题】:How to dynamically order by certain entity properties in Entity Framework 7 (Core)如何在 Entity Framework 7 (Core) 中按某些实体属性动态排序
【发布时间】:2016-07-17 20:50:35
【问题描述】:

我有一个项目,其中前端 JavaScript 指定要排序的列列表。

然后在后端我有多层应用程序。典型场景

  1. 服务层(服务模型的 (DTO) 属性匹配客户端想要订购的任何东西)
  2. 域层(它公开存储库接口以访问持久对象)
  3. ORM 层(它实现存储库并使用 Entity Framework 7(也称为 Entity Framework Core)来访问 SQL Server 数据库)

请注意,DNX Core v5.0 或 .NET Platform v5.4 不支持 System.Linq.Dynamic,因此我无法使用该库。

我的Things 存储库中有以下实现:

    public async Task<IEnumerable<Thing>> GetThingsAsync(IEnumerable<SortModel> sortModels)
    {
        var query = GetThingsQueryable(sortModels);
        var things = await query.ToListAsync();
        return things;
    }

    private IQueryable<Thing> GetThingsQueryable(IEnumerable<SortModel> sortModels)
    {

        var thingsQuery = _context.Things
                .Include(t => t.Other)
                .Where(t => t.Deleted == false);

        // this is the problematic area as it does not return a valid queryable
        string orderBySqlStatement = GetOrderBySqlStatement(sortModels);
        thingsQuery = thingsQuery.FromSql(orderBySqlStatement);
        return thingsQuery ;
    }

    /// this returns something like " order by thingy1 asc, thingy2 desc"
    private string GetOrderBySqlStatement(IEnumerable<SortModel> sortModels)
    {
        IEnumerable<string> orderByParams = sortModels.Select(pair => { return pair.PairAsSqlExpression; });
        string orderByParamsConcat = string.Join(", ", orderByParams);
        string sqlStatement = orderByParamsConcat.Length > 1 ? $" order by {orderByParamsConcat}" : string.Empty;
        return sqlStatement;
    }

这是包含列名和方向顺序(asc 或 desc)的对象

public class SortModel
{
    public string ColId { get; set; }
    public string Sort { get; set; }

    public string PairAsSqlExpression
    {
        get
        {
            return $"{ColId} {Sort}";
        }
    }
}

这种方法尝试将 SQL 语句与 Entity 为之前的可查询对象创建的任何内容混合。但我得到一个:

Microsoft.Data.Entity.Query.Internal.SqlServerQueryCompilationContextFactory:Verbose: Compiling query model: 'from Thing t in {value(Microsoft.Data.Entity.Query.Internal.EntityQueryable`1[MyTestProj.Data.Models.Thing]) => AnnotateQuery(Include([t].DeparturePort)) => AnnotateQuery(Include([t].ArrivalPort)) => AnnotateQuery(Include([t].Consignments))} where (([t].CreatorBusinessId == __businessId_0) AndAlso (Convert([t].Direction) == __p_1)) select [t] => AnnotateQuery(QueryAnnotation(FromSql(value(Microsoft.Data.Entity.Query.Internal.EntityQueryable`1[MyTestProj.Data.Models.Thing]), " order by arrivalDate asc, arrivalPortCode asc", []))) => Count()'
Microsoft.Data.Entity.Query.Internal.SqlServerQueryCompilationContextFactory:Verbose: Optimized query model: 'from Thing t in value(Microsoft.Data.Entity.Query.Internal.EntityQueryable`1[MyTestProj.Data.Models.Thing]) where (([t].CreatorBusinessId == __businessId_0) AndAlso (Convert([t].Direction) == __p_1)) select [t] => Count()'
Microsoft.Data.Entity.Query.Internal.QueryCompiler:Error: An exception occurred in the database while iterating the results of a query.
System.InvalidOperationException: The Include operation is not supported when calling a stored procedure.
   at Microsoft.Data.Entity.Query.ExpressionVisitors.RelationalEntityQueryableExpressionVisitor.VisitEntityQueryable(Type elementType)
   at Microsoft.Data.Entity.Query.ExpressionVisitors.EntityQueryableExpressionVisitor.VisitConstant(ConstantExpression constantExpression)
   at System.Linq.Expressions.ConstantExpression.Accept(ExpressionVisitor visitor)
   at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
   at Microsoft.Data.Entity.Query.ExpressionVisitors.ExpressionVisitorBase.Visit(Expression expression)
   at Microsoft.Data.Entity.Query.EntityQueryModelVisitor.ReplaceClauseReferences(Expression expression, IQuerySource querySource, Boolean inProjection)
   at Microsoft.Data.Entity.Query.EntityQueryModelVisitor.CompileMainFromClauseExpression(MainFromClause mainFromClause, QueryModel queryModel)
   at Microsoft.Data.Entity.Query.RelationalQueryModelVisitor.CompileMainFromClauseExpression(MainFromClause mainFromClause, QueryModel queryModel)
   at Microsoft.Data.Entity.Query.EntityQueryModelVisitor.VisitMainFromClause(MainFromClause fromClause, QueryModel queryModel)
   at Remotion.Linq.Clauses.MainFromClause.Accept(IQueryModelVisitor visitor, QueryModel queryModel)
   at Remotion.Linq.QueryModelVisitorBase.VisitQueryModel(QueryModel queryModel)
   at Microsoft.Data.Entity.Query.EntityQueryModelVisitor.VisitQueryModel(QueryModel queryModel)
   at Microsoft.Data.Entity.Query.RelationalQueryModelVisitor.VisitQueryModel(QueryModel queryModel)
   at Microsoft.Data.Entity.Query.Internal.SqlServerQueryModelVisitor.VisitQueryModel(QueryModel queryModel)
   at Microsoft.Data.Entity.Query.EntityQueryModelVisitor.CreateAsyncQueryExecutor[TResult](QueryModel queryModel)
   at Microsoft.Data.Entity.Storage.Database.CompileAsyncQuery[TResult](QueryModel queryModel)
   at Microsoft.Data.Entity.Query.Internal.QueryCompiler.<>c__DisplayClass19_0`1.<CompileAsyncQuery>b__0()
   at Microsoft.Data.Entity.Query.Internal.CompiledQueryCache.GetOrAddAsyncQuery[TResult](Object cacheKey, Func`1 compiler)
   at Microsoft.Data.Entity.Query.Internal.QueryCompiler.CompileAsyncQuery[TResult](Expression query)
   at Microsoft.Data.Entity.Query.Internal.QueryCompiler.ExecuteAsync[TResult](Expression query, CancellationToken cancellationToken)
Exception thrown: 'System.InvalidOperationException' in EntityFramework.Core.dll
Exception thrown: 'System.InvalidOperationException' in mscorlib.ni.dll
Exception thrown: 'System.InvalidOperationException' in mscorlib.ni.dll
Microsoft.AspNet.Diagnostics.Entity.DatabaseErrorPageMiddleware:Verbose: System.InvalidOperationException occurred, checking if Entity Framework recorded this exception as resulting from a failed database operation.
Microsoft.AspNet.Diagnostics.Entity.DatabaseErrorPageMiddleware:Verbose: Entity Framework recorded that the current exception was due to a failed database operation. Attempting to show database error page.
Microsoft.Data.Entity.Storage.Internal.SqlServerConnection:Verbose: Opening connection 'Server=(localdb)\mssqllocaldb;Database=SpeediCargo;Trusted_Connection=True;MultipleActiveResultSets=true'.
Microsoft.Data.Entity.Storage.Internal.SqlServerConnection:Verbose: Closing connection 'Server=(localdb)\mssqllocaldb;Database=SpeediCargo;Trusted_Connection=True;MultipleActiveResultSets=true'.
Microsoft.Data.Entity.Storage.Internal.SqlServerConnection:Verbose: Opening connection 'Server=(localdb)\mssqllocaldb;Database=SpeediCargo;Trusted_Connection=True;MultipleActiveResultSets=true'.
Microsoft.Data.Entity.Storage.Internal.SqlServerConnection:Verbose: Closing connection 'Server=(localdb)\mssqllocaldb;Database=SpeediCargo;Trusted_Connection=True;MultipleActiveResultSets=true'.
Microsoft.Data.Entity.Storage.Internal.SqlServerConnection:Verbose: Opening connection 'Server=(localdb)\mssqllocaldb;Database=SpeediCargo;Trusted_Connection=True;MultipleActiveResultSets=true'.
Microsoft.Data.Entity.Storage.Internal.RelationalCommandBuilderFactory:Information: Executed DbCommand (0ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
SELECT OBJECT_ID(N'__EFMigrationsHistory');
Microsoft.Data.Entity.Storage.Internal.SqlServerConnection:Verbose: Closing connection 'Server=(localdb)\mssqllocaldb;Database=SpeediCargo;Trusted_Connection=True;MultipleActiveResultSets=true'.
Microsoft.Data.Entity.Storage.Internal.SqlServerConnection:Verbose: Opening connection 'Server=(localdb)\mssqllocaldb;Database=SpeediCargo;Trusted_Connection=True;MultipleActiveResultSets=true'.
Microsoft.Data.Entity.Storage.Internal.RelationalCommandBuilderFactory:Information: Executed DbCommand (0ms) [Parameters=[], CommandType='Text', CommandTimeout='30']

似乎不可能将订单的 SQL 部分与 linq 查询的其余部分混合在一起? 或者问题是我没有在列前加上 [t] 并且实体无法理解这些列是什么?

无论如何,关于如何使用 Entity 7 和核心 .net 框架实现我想要的目标,有什么示例或建议吗?

【问题讨论】:

  • 我希望应用程序的某些部分正在检查类似GetThingsQueryable(new List&lt;SortModel&gt;() { new SortModel() { ColId = "A", Sort = "; DROP TABLE Students" })
  • 我同意 Eric,我应该注意 SQL 注入
  • 我认为 FromSql 需要完整的 SQL 语句。而且它不会与以前的“混合”。
  • 你的问题和答案对我帮助很大,你有没有这么好的解决方案来寻找线索。扩展排序可以节省我编写数千行代码的时间。不,我想在哪里尝试类似的东西。我的网格还向我发送了动态过滤器,在我尝试重新发明轮子之前,我问你有没有针对此的片段。感谢先进

标签: linq sql-order-by asp.net-core dynamic-sql entity-framework-core


【解决方案1】:

FromSql绝对不能用来混SQL。因此,像往常一样使用动态查询,您必须求助于System.Linq.Expressions

例如,像这样的:

public static class QueryableExtensions
{
    public static IQueryable<T> OrderBy<T>(this IQueryable<T> source, IEnumerable<SortModel> sortModels)
    {
        var expression = source.Expression;
        int count = 0;
        foreach (var item in sortModels)
        {
            var parameter = Expression.Parameter(typeof(T), "x");
            var selector = Expression.PropertyOrField(parameter, item.ColId);
            var method = string.Equals(item.Sort, "desc", StringComparison.OrdinalIgnoreCase) ?
                (count == 0 ? "OrderByDescending" : "ThenByDescending") :
                (count == 0 ? "OrderBy" : "ThenBy");
            expression = Expression.Call(typeof(Queryable), method,
                new Type[] { source.ElementType, selector.Type },
                expression, Expression.Quote(Expression.Lambda(selector, parameter)));
            count++;
        }
        return count > 0 ? source.Provider.CreateQuery<T>(expression) : source;
    }
}

然后:

var thingsQuery = _context.Things
        .Include(t => t.Other)
        .Where(t => t.Deleted == false)
        .OrderBy(sortModels);

【讨论】:

  • 是否有任何样本显示延伸到哪里的线索。
  • @adopilot 不确定我是否遵循这个问题。你要动态Where吗?
  • 是的,我想尝试像你这样的 bulid extenzion,但要寻找线索。我想尝试动态地执行您向我们展示动态排序的方式。
  • @adopilot Where 谓词更简单,因为已知它们会返回bool,并且一旦创建,就可以直接使用Where 方法。这是一个例子:stackoverflow.com/questions/39182903/….
猜你喜欢
  • 2020-01-13
  • 2018-06-09
  • 1970-01-01
  • 2018-02-05
  • 1970-01-01
  • 2017-08-18
  • 1970-01-01
  • 2023-03-29
  • 1970-01-01
相关资源
最近更新 更多