简体   繁体   English

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

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

I want to do query in EntityFramework Core 3.1我想在 EntityFramework Core 3.1 中进行查询

This is my query:这是我的查询:

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

But not works, this the message error但不起作用,这是消息错误

Lambda expression used inside Include is not valid.

This is de message StackTrace这是消息 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 visitor) at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node) at Microsoft.EntityFrameworkCore.Query.Internal.NavigationExpandingExpressionVisitor.Expand(Expression query) at Microsoft.EntityFrameworkCore.Query.QueryTranslationPreprocessor.Process(Expression query) at Microsoft.EntityFrameworkCore.Query.QueryCompilationContext.CreateQueryExecutor[TResult](Expression query) at Microsoft.EntityFrameworkCore.Storage.Database.CompileQuery[TResult](Expression query, Z27226C864BAC7454A8504F8EDB15D95B 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 async) at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.CompileQueryCore[TResult](IDatabase database, Expression query, IModel model, Boolean async) at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass12_0 1.<ExecuteAsync>b__0() at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQueryCore[TFunc](Object cacheKey, Func 1 compiler) at 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.GetAsyncEnumerator(CancellationToken cancellationToken) at Microsoft 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() at 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() at Business.Implementation.BsCategoriaPregunta.d__4.MoveNext() in C:\Users\admin\source\repos\API013-Ayuda\Business\Implementation\BsCategoriaPregunta.cs:line 58 .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

Any idea...???任何想法...???

The purpose of Include() is to tell it to load data from a related table at the same time that this query is processed. Include()的目的是告诉它在处理此查询的同时从相关表中加载数据。

The example in the documentation uses a scenario where you want to load a list of blogs from the database and also include the posts related to each blog: 文档中的示例使用了一个场景,您希望从数据库中加载博客列表,并且还包括与每个博客相关的帖子:

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

So it would not make sense to have a Where() inside Include() .所以在Include() Where()是没有意义的。

You will have to tell us what you are trying to do.您必须告诉我们您要做什么。 But maybe something like this might work?但也许这样的事情可能会奏效?

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

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

相关问题 Entity Framework Core 3.0 - Include 中使用的 Lambda 表达式无效 - Entity Framework Core 3.0 - Lambda expression used inside Include is not valid EF Core 过滤包含:“包含中使用的 Lambda 表达式无效” - EF Core Filtered Include: “Lambda expression used inside Include is not valid” EF Core:在 Include 中使用的 Lambda 表达式无效 - EF Core: Lambda expression used inside Include is not valid 在Include中使用的Lambda表达式无效 - Lambda expression used inside Include is not valid EF Core 3.1 - 包含过滤抛出错误“包含内部使用的 Lambda 表达式无效。” - EF Core 3.1 - Include filtering throw an error 'Lambda expression used inside Include is not valid.' Include 中使用的 Lambda 表达式无效。 包括不工作 - Lambda expression used inside Include is not valid. Include not working EF 核心 ThenInclude Select 说“Include 中使用的 Lambda 表达式无效” - EF core ThenInclude Select says "Lambda expression used inside Include is not valid" EF Core lambda expr 内包含无效 - EF Core lambda expr inside include not valid Include 内部使用的 Lambda 表达式无效。 EF6,导航属性 - Lambda expression used inside Include is not valid. EF6, Navigation Property 我在实体框架中找不到带有lambda表达式的“包含”方法吗? - I can't find “Include” method with lambda expression in Entity framework?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM