【问题标题】:Lambda expression used inside Include is not valid in Entity Framework CoreInclude 中使用的 Lambda 表达式在 Entity Framework Core 中无效
【发布时间】:2020-09-30 03:24:08
【问题描述】:

我想在 EntityFramework Core 3.1 中进行查询

这是我的查询:

return await context_client.Tbcategoriapregunta.Include(e => e.Tbpreguntasfrecuentes.Where(preguntas => preguntas.Dstipousuario == user)).ToListAsync();

但不起作用,这是消息错误

Lambda expression used inside Include is not valid.

这是消息 StackTrace

在 Microsoft.EntityFrameworkCore.Query.Internal.NavigationExpandingExpressionVisitor.ProcessInclude(NavigationExpansionExpression 源,表达式表达式,布尔 thenInclude) 在 Microsoft.EntityFrameworkCore.Query.Internal.NavigationExpandingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression) 在 System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor 访问者) 在 System.Linq.Expressions.ExpressionVisitor.Visit(表达式节点) 在 Microsoft.EntityFrameworkCore.Query.Internal.NavigationExpandingExpressionVisitor.Expand(表达式查询) 在 Microsoft.EntityFrameworkCore.Query.QueryTranslationPreprocessor.Process(表达式查询) 在 Microsoft.EntityFrameworkCore.Query.QueryCompilationContext.CreateQueryExecutor[TResult](表达式查询) 在 Microsoft.EntityFrameworkCore.Storage.Database.CompileQuery[TResult](表达式查询,布尔异步) 在 Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.CompileQueryCore[TResult](IDatabase 数据库,表达式查询,IModel 模型,布尔异步) 在 Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.c__DisplayClass12_01.<ExecuteAsync>b__0() at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQueryCore[TFunc](Object cacheKey, Func1 编译器) 在 Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQuery[TResult](对象 cacheKey,Func1 compiler) at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.ExecuteAsync[TResult](Expression query, CancellationToken cancellationToken) at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.ExecuteAsync[TResult](Expression expression, CancellationToken cancellationToken) at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable1.GetAsyncEnumerator(CancellationToken cancelToken) 在 Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.IncludableQueryable2.GetAsyncEnumerator(CancellationToken cancellationToken) at System.Runtime.CompilerServices.ConfiguredCancelableAsyncEnumerable1.GetAsyncEnumerator() 在 Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.d__641.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter1.GetResult() 在 C:\Users\admin\source\repos\API013-Ayuda\Business\Implementation\BsCategoriaPregunta.cs:line 58 中的 Business.Implementation.BsCategoriaPregunta.d__4.MoveNext() 处

任何想法...???

【问题讨论】:

    标签: c# sql postgresql asp.net-core entity-framework-core


    【解决方案1】:

    Include() 的目的是告诉它在处理此查询的同时从相关表中加载数据。

    the documentation 中的示例使用了一个场景,您希望从数据库中加载博客列表并包含与每个博客相关的帖子:

    using (var context = new BloggingContext())
    {
        var blogs = context.Blogs
            .Include(blog => blog.Posts)
            .ToList();
    }
    

    因此在Include() 中包含Where() 是没有意义的。

    您必须告诉我们您想要做什么。但也许这样的事情可能会奏效?

    return await context_client.Tbcategoriapregunta
        .Include(e => e.Tbpreguntasfrecuentes)
        .Where(categoria => categoria.Tbpreguntasfrecuentes.Dstipousuario == user)
        .ToListAsync();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-31
      • 1970-01-01
      • 1970-01-01
      • 2020-04-28
      • 1970-01-01
      • 2020-03-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多