繁体   English   中英

在 Include 中使用的 Lambda 表达式在 Entity Framework Core 中无效

[英]Lambda expression used inside Include is not valid in Entity Framework Core

我想在 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

at Microsoft.EntityFrameworkCore.Query.Internal.NavigationExpandingExpressionVisitor.ProcessInclude(NavigationExpansionExpression source, Expression expression, Boolean thenInclude) at Microsoft.EntityFrameworkCore.Query.Internal.NavigationExpandingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression) at 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](表达式查询,Z27226C864BAC7454A8504F8EDB15D95B Z 异步)在 Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.CompileQueryCore[TResult](IDatabase 数据库,表达式查询,IModel model,Boolean 异步)在 Microsoft.EntityFrameworkCore<c__DisplayClass1>ExecAsync.<c__DisplayClass1 1.<ExecuteAsync>b__0() at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQueryCore[TFunc](Object cacheKey, Func 1 编译器) 在 Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQuery[TResult](Object cacheKey, Func 1 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.EntityQueryable微软1 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.EntityQueryable .EntityQueryable 1.GetAsyncEnumerator(CancellationToken cancelToken) .EntityFrameworkCore.EntityFrameworkQueryableExtensions.IncludableQueryable 2.GetAsyncEnumerator(CancellationToken cancellationToken) at System.Runtime.CompilerServices.ConfiguredCancelableAsyncEnumerable 1.GetAsyncEnumerator() 在 Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.d__64 1.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.TaskAwaiter 1.GetResult() 在 Business.Implementation.BsCategoriaPregunta.d__4 .MoveNext() 在 C:\Users\admin\source\repos\API013-Ayuda\Business\Implementation\BsCategoriaPregunta.cs:line 58

任何想法...???

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM