我只想使用IQueryable
从 EF Core 获取的内容来根据过滤器过滤客户,在这种情况下,过滤器是另一个客户串列。
经过数小时的搜索,我发现了这个,它看起来作业正常,然后我尝试这样做:
List<Customer> filteredCustomers = iqueryable.Join(customersListAsFilter.Select(F => F.Id), F => F.Id, Id => Id, (T, S) => T).ToList<Customer>();
并得到了这个神秘的错误:
System.InvalidOperationException
HResult=0x80131509
讯息=处理 LINQ 表达式 'DbSet .Join(outer: __p_0, inner: F => F.Id, outerKeySelector: Id => Id, innerKeySelector: (T, S) => T)' 'NavigationExpandingExpressionVisitor' 失败。这可能表示 EF Core 中的错误或限制。有关更多详细信息,请参阅https://go.microsoft.com/fwlink/?linkid=2101433。
源=Microsoft.EntityFrameworkCore堆栈跟踪:
在 Microsoft.EntityFrameworkCore.Query.Internal.NavigationExpandingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
在 System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor)
在 System.Linq.Expressions.ExpressionVisitor.Visit(表达式节点)
在 Microsoft.EntityFrameworkCore.Query.Internal.NavigationExpandingExpressionVisitor.Expand(Expression query) 在 Microsoft.EntityFrameworkCore.Query.QueryTranslationPreprocessor.Process(Expression query) 在 Microsoft.EntityFrameworkCore.Query.QueryCompilationContext.CreateQueryExecutor[TResult](Expression query) 在 Microsoft。 Microsoft.EntityFrameworkCore.EntityFrameworkCore.Query.Internal.QueryCompiler.CompileQueryCore[TResult](IDatabase 数据库,表达式查询,IModel 模型,布尔异步)中的 EntityFrameworkCore.Storage.Database.CompileQuery[TResult](表达式查询,布尔异步)。 Query.Internal.QueryCompiler.<>c__DisplayClass9_01.<Execute>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.Execute[TResult](Expression query) at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.Execute[TResult](Expression expression) at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable
1.GetEnumerator() 在 System.Collections.Generic.List1..ctor(IEnumerable
1 集合)在 System.Linq.Enumerable.ToList[TSource](IEnumerable1 source) at MyProjectDir.shared.MyDbClass.Read(List
1 个客户)在 C:\MyProjectDir\shared\MyDbClass.cs:第 35 行在 MyProjectDir.shared.MyDbClass.Read(客户客户)在 C:\MyProjectDir\shared\MyDbClass.cs:在 WTAPP.Controllers.CustomerController 的第 42 行。 Get(Int32 Id) in C:\MyProjectDir\MyWebProj\CustomerController.cs:line 37 at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.SyncObjectResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments) 在 Microsoft .AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeActionMethodAsync() 在 Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted) 在 Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeNextActionFilterAsync( )
Another alternative I found was this and I tried this:
var result = q.GroupBy(target => target.Id)
.Where(target => target.Select(T => T.Id)
.OrderBy(Id => Id)
.SequenceEqual(customersListAsFilter));
But in this case, I get this error:
Error CS1929
'IOrderedEnumerable' does not contain a definition for 'SequenceEqual' and the best extension method overload 'ParallelEnumerable.SequenceEqual(ParallelQuery, IEnumerable)' requires a receiver of type 'ParallelQuery'
uj5u.com热心网友回复:
假设您的iqueryable
变量是IQueryable<Customer>
,并且您正在尝试根据您在 中的内容获取一部分客户customersListAsFilter
,您可以执行以下操作:
List<int> customersToGet = customersListAsFilter.Select(x => x.Id).ToList();
List<Customer> filteredCustomers = iqueryable.Where(x => customersToGet.Contains(x.Id)).ToList();
生成的查询将类似于:
select * -- (column names)
from Customers
where Id in (1,2,3,4,5,6) -- the IDs from customersToGet
0 评论