获取嵌套集合包含项目的项目形成另一个集合

时间:2016-02-07 02:57:48

标签: c# .net entity-framework linq entity-framework-core

我正在尝试获取项目,其中嵌套集合包含来自另一个集合的项目或多个项目。如果我首先枚举集合然后执行内存中.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的一个问题?或者我做错了什么?感谢。

1 个答案:

答案 0 :(得分:2)

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

已知问题,设置为使用EF的发布版本修复。