[英]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, 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.EntityQueryable
1.GetAsyncEnumerator(CancellationToken cancellationToken) at MicrosoftZ 异步)在 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, 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.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.IncludableQueryable2.GetAsyncEnumerator(CancellationToken cancellationToken) at System.Runtime.CompilerServices.ConfiguredCancelableAsyncEnumerable
1.GetAsyncEnumerator() at 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.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__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.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.