獲取嵌套集合包含項目的項目形成另一個集合

.net c# entity-framework entity-framework-core linq

我正在嘗試獲取項目,其中嵌套集合包含項目或來自另一個集合的多個項目。如果我首先枚舉集合然後執行內存中.Where()過濾器,我會得到預期的集合。

var filterTagIds = new[] { 1, 2, 3 };

var works = base.DbSet
    .Include(p => p.Company)
        .ThenInclude(p => p.CompanyTags)
    .ToList()
    .Where(p => p.Company.CompanyTags.Any(q => filterTagIds.Contains(q.TagId)))
    .ToList();

但是,如果我嘗試在沒有枚舉的情況下執行此操作(我想在數據庫級別上執行此操作),則會出現異常。

var doesNotWork = base.DbSet
    .Include(p => p.Company)
        .ThenInclude(p => p.CompanyTag)
    .Where(p => p.Company.CompanyTags.Any(q => filterTagIds.Contains(q.TagId)))
    .ToList();

這是我得到的例外:

{"variable 't0' of type 'Microsoft.Data.Entity.Query.EntityQueryModelVisitor+TransparentIdentifier`2[Microsoft.Data.Entity.Storage.ValueBuffer,Microsoft.Data.Entity.Storage.ValueBuffer]' referenced from scope '', but it is not defined"}
    Data: {System.Collections.ListDictionaryInternal}
    HResult: -2146233079
    HelpLink: null
    InnerException: null
    Message: "variable 't0' of type 'Microsoft.Data.Entity.Query.EntityQueryModelVisitor+TransparentIdentifier`2[Microsoft.Data.Entity.Storage.ValueBuffer,Microsoft.Data.Entity.Storage.ValueBuffer]' referenced from scope '', but it is not defined"
    Source: "System.Core"
    StackTrace: "   at System.Linq.Expressions.Compiler.VariableBinder.Reference(ParameterExpression node, VariableStorageKind storage)\r\n   at System.Linq.Expressions.Compiler.VariableBinder.VisitParameter(ParameterExpression node)\r\n   at System.Linq.Expressions.ParameterExpression.Accept(ExpressionVisitor visitor)\r\n   at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)\r\n   at System.Linq.Expressions.ExpressionVisitor.VisitMember(MemberExpression node)\r\n   at System.Linq.Expressions.MemberExpression.Accept(ExpressionVisitor visitor)\r\n   at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)\r\n   at System.Linq.Expressions.ExpressionVisitor.VisitMethodCall(MethodCallExpression node)\r\n   at System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor)\r\n   at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)\r\n   at System.Linq.Expressions.Compiler.VariableBinder.VisitUnary(UnaryExpression node)\r\n   at System.Linq.Expressions.UnaryEx
pression.Accept(ExpressionVisitor visitor)\r\n   at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)\r\n   at System.Linq.Expressions.ExpressionVisitor.Visit(ReadOnlyCollection`1 nodes)\r\n   at System.Linq.Expressions.Compiler.VariableBinder.VisitLambda[T](Expression`1 node)\r\n   at System.Linq.Expressions.Expression`1.Accept(ExpressionVisitor visitor)\r\n   at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)\r\n   at System.Linq.Expressions.ExpressionVisitor.VisitArguments(IArgumentProvider nodes)\r\n   at System.Linq.Expressions.ExpressionVisitor.VisitMethodCall(MethodCallExpression node)\r\n   at System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor)\r\n   at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)\r\n   at System.Linq.Expressions.ExpressionVisitor.VisitArguments(IArgumentProvider nodes)\r\n   at System.Linq.Expressions.ExpressionVisitor.VisitMethodCall(MethodCallExpression node)\r\n   at System.Linq.Expressions.MethodCallExp
ression.Accept(ExpressionVisitor visitor)\r\n   at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)\r\n   at System.Linq.Expressions.ExpressionVisitor.VisitArguments(IArgumentProvider nodes)\r\n   at System.Linq.Expressions.ExpressionVisitor.VisitMethodCall(MethodCallExpression node)\r\n   at System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor)\r\n   at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)\r\n   at System.Linq.Expressions.ExpressionVisitor.VisitArguments(IArgumentProvider nodes)\r\n   at System.Linq.Expressions.ExpressionVisitor.VisitMethodCall(MethodCallExpression node)\r\n   at System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor)\r\n   at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)\r\n   at System.Linq.Expressions.ExpressionVisitor.Visit(ReadOnlyCollection`1 nodes)\r\n   at System.Linq.Expressions.Compiler.VariableBinder.VisitLambda[T](Expression`1 node)\r\n   at System.Linq.Expressions.Expres
sion`1.Accept(ExpressionVisitor visitor)\r\n   at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)\r\n   at System.Linq.Expressions.ExpressionVisitor.VisitArguments(IArgumentProvider nodes)\r\n   at System.Linq.Expressions.ExpressionVisitor.VisitMethodCall(MethodCallExpression node)\r\n   at System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor)\r\n   at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)\r\n   at System.Linq.Expressions.ExpressionVisitor.VisitArguments(IArgumentProvider nodes)\r\n   at System.Linq.Expressions.ExpressionVisitor.VisitMethodCall(MethodCallExpression node)\r\n   at System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor)\r\n   at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)\r\n   at System.Linq.Expressions.ExpressionVisitor.VisitArguments(IArgumentProvider nodes)\r\n   at System.Linq.Expressions.ExpressionVisitor.VisitMethodCall(MethodCallExpression node)\r\n   at System.Linq.Expression
s.MethodCallExpression.Accept(ExpressionVisitor visitor)\r\n   at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)\r\n   at System.Linq.Expressions.ExpressionVisitor.Visit(ReadOnlyCollection`1 nodes)\r\n   at System.Linq.Expressions.Compiler.VariableBinder.VisitLambda[T](Expression`1 node)\r\n   at System.Linq.Expressions.Expression`1.Accept(ExpressionVisitor visitor)\r\n   at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)\r\n   at System.Linq.Expressions.Compiler.VariableBinder.Bind(LambdaExpression lambda)\r\n   at System.Linq.Expressions.Compiler.LambdaCompiler.Compile(LambdaExpression lambda, DebugInfoGenerator debugInfoGenerator)\r\n   at System.Linq.Expressions.Expression`1.Compile()\r\n   at Microsoft.Data.Entity.Query.EntityQueryModelVisitor.CreateExecutorLambda[TResults]()\r\n   at Microsoft.Data.Entity.Query.EntityQueryModelVisitor.CreateQueryExecutor[TResult](QueryModel queryModel)\r\n   at Microsoft.Data.Entity.Storage.Database.CompileQuery[TResult](QueryModel quer
yModel)\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   at Microsoft.Data.Entity.Query.Internal.QueryCompiler.<>c__DisplayClass18_0`1.<CompileQuery>b__0()\r\n   at Microsoft.Data.Entity.Query.Internal.CompiledQueryCache.GetOrAddQuery[TResult](Object cacheKey, Func`1 compiler)\r\n   at Microsoft.Data.Entity.Query.Internal.QueryCompiler.CompileQuery[TResult](Expression query)\r\n   at Microsoft.Data.Entity.Query.Internal.QueryCompiler.Execute[TResult](Expression query)\r\n   at Microsoft.Data.Entity.Query.Internal.EntityQueryProvider.Execute[TResult](Expression expression)\r\n   at Remotion.Linq.QueryableBase`1.GetEnumerator()\r\n   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)\r\n   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)\r\n   at MyProject.DAL.Repository.LocationRepository.GetCompanyLocations(CompanyMarkersFilter filter) in D:\\projects\\MyProject\\src\\Komp
anio.DAL\\Repository\\LocationRepository.cs:line 24"
    TargetSite: {Void Reference(System.Linq.Expressions.ParameterExpression, System.Linq.Expressions.Compiler.VariableStorageKind)}

我正在尋找在EF中可行的東西嗎?也許這只是EF-Core的一個問題?或者我做錯了什麼?謝謝。

一般承認的答案

https://github.com/aspnet/EntityFramework/issues/4596

已知問題,設​​置為使用EF的發布版本修復。



許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因
許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因