Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# EF7/Linq-“;包括「;具有嵌套对象的查询引发异常_C#_Entity Framework_Linq_Entity Framework Core - Fatal编程技术网

C# EF7/Linq-“;包括「;具有嵌套对象的查询引发异常

C# EF7/Linq-“;包括「;具有嵌套对象的查询引发异常,c#,entity-framework,linq,entity-framework-core,C#,Entity Framework,Linq,Entity Framework Core,我有以下课程: public class ActivityEntry { public int Id { get; set; } public int CostCenterId { get; set; } [ForeignKey("CostCenterId")] public CostCenter CostCenter { get; set; } } public class CostCenter { public int Id {ge

我有以下课程:

public class ActivityEntry 
{      
    public int Id { get; set; }

    public int CostCenterId { get; set; }
    [ForeignKey("CostCenterId")]
    public CostCenter CostCenter { get; set; }
}

public class CostCenter 
{
    public int Id {get;set;}
    public int ActivityId { get; set; }
}
我想查询所有
ActivityEntries
,其中
CostCenter
具有特定的
activityid

对于过滤器,我有一个带有ActivityID的列表(
filterList
)。以下查询抛出一个
系统。InvalidOperationException

“序列包含多个元素”

我不明白为什么

var filterList = new List<int> {1};

var data = _db.ActivityEntry
              .Include(x => x.CostCenter)
              .Where(x => filterList.Contains(x.CostCenter.ActivityId))
              .ToList();
我是否需要使用其他语法来查询
CostCenter
对象中的活动ID

堆栈跟踪:

bei System.Linq.Enumerable.Single[TSource](IEnumerable
1源)
bei Microsoft.Data.Entity.Query.EntityQueryModelVisitor.c__显示类79_0
1.b_0(IEnumerable
1PS,IQuerySource qs)
bei Microsoft.Data.Entity.Query.EntityQueryModelVisitor.BindMemberExpressionCore[TResult](MemberExpression MemberExpression,IQuerySource querySource,Func
3 memberBinder) bei Microsoft.Data.Entity.Query.EntityQueryModelVisitor.BindMemberExpression[TResult](MemberExpression MemberExpression,IQuerySource querySource,Func
3 memberBinder)
bei Microsoft.Data.Entity.Query.EntityQueryModelVisitor.BindMemberExpression(MemberExpression MemberExpression,Action
2 memberBinder) bei Microsoft.Data.Entity.Query.ExpressionVisitors.Internal.RequiresMaterializationExpressionVisitor.VisitMember(MemberExpression MemberExpression) bei系统.Linq.Expressions.MemberExpression.Accept(ExpressionVisitor) bei Microsoft.Data.Entity.Query.ExpressionVisitors.ExpressionVisitorBase.Visit(表达式) bei Remotion.Linq.子句.ResultOperators.ContainsResultOperator.TransformExpressions(Func
2转换)
bei Remotion.Linq.QueryModel.TransformExpressions(Func
2转换) bei Microsoft.Data.Entity.Query.ExpressionVisitors.Internal.RequiresMaterializationExpressionVisitor.VisitSubQueryExpression查询(SubQueryExpression SubQueryExpression) bei Remotion.Linq.子句.Expressions.SubQueryExpression.Accept(ExpressionVisitor) bei Microsoft.Data.Entity.Query.ExpressionVisitors.ExpressionVisitorBase.Visit(表达式) bei Remotion.Linq.子句.where子句.TransformExpressions(Func
2转换)
bei Remotion.Linq.QueryModel.TransformExpressions(Func
2转换) bei Microsoft.Data.Entity.Query.ExpressionVisitors.Internal.RequiresMaterializationExpressionVisitor.FindQuerySourcesRequiringMaterialization(QueryModel QueryModel QueryModel) bei Microsoft.Data.Entity.Query.QueryCompilationContext.FindQuerySourcesRequiringMaterialization(EntityQueryModelVisitor queryModelVisitor,QueryModelQueryModel QueryModel) bei Microsoft.Data.Entity.Query.EntityQueryModelVisitor.CreateQueryExecutor[TResult](QueryModel QueryModel) bei Microsoft.Data.Entity.Storage.Database.CompileQuery[TResult](QueryModel QueryModel) ---这是一个非常有趣的故事--- bei Microsoft.Data.Entity.Query.Internal.QueryCompiler.c_uudisplayClass18_0
1.b_u0()
bei Microsoft.Data.Entity.Query.Internal.CompiledQueryCache.GetOradQuery[TResult](对象缓存键,Func
1编译器) bei Microsoft.Data.Entity.Query.Internal.QueryCompiler.CompileQuery[TResult](表达式查询) bei Microsoft.Data.Entity.Query.Internal.QueryCompiler.Execute[TResult](表达式查询) bei Microsoft.Data.Entity.Query.Internal.EntityQueryProvider.Execute[TResult](表达式) bei Remotion.Linq.QueryableBase
1.GetEnumerator()
bei系统.Collections.Generic.List
1..ctor(IEnumerable
1 collection)
bei系统.Linq.Enumerable.ToList[TSource](IEnumerable
1源) C:\Source\zetvtext\src\zetvtext.application\Services\ReportService.cs:Zeile 86中的bei zetvtext.application.Services.ReportService.GetCostCenterReportData(filterTo filter)。 bei zetvtext.tests.ReportServiceTests.Test_Report_Data()在C:\Source\zetvtext\tests\zetvText.tests\ReportServiceTests.cs:Zeile 142中


这是EF7的RC1中的错误,已在RC2中修复


=>

这是EF7的RC1中的错误,已在RC2中修复


=>

ActivityEntry似乎不包含员工属性。我怀疑这是问题所在,但这让我想知道你还没有展示什么…:-)在构造上述查询时通常不会出现错误-如果要生成错误,则通常在执行时会发生错误,例如通过调用
ToList()
。引用的特定异常可能来自
Single()
。你能告诉我们结果是如何具体化的(即如何使用查询的结果)吗?@Gary McGill添加了该员工(我试图在这里发布SO时只包含理解问题所需的部分;-)+添加了ToList()-遗漏-执行时引发异常。您能否演示如何声明和初始化
filter
变量,以及堆栈跟踪的更多内容?我仍然怀疑
ToList()
是否会给出该错误,而不是
Single()
SingleOrDefault()
它这样工作的原因是它将整个表加载到内存中,并在那里应用
Where
子句。这样做是不好的:)ActivityEntry似乎不包含Employee属性。我怀疑这是问题所在,但这让我想知道你还没有展示什么…:-)在构造上述查询时通常不会出现错误-如果要生成错误,则通常在执行时会发生错误,例如通过调用
ToList()
。引用的特定异常可能来自
Single()
。你能告诉我们结果是如何具体化的(即查询结果是如何使用的)吗?@Gary McGill添加了该员工(我尝试只包括以下所需的部分
.Where(x =>filterList.Contains(x.CostCenterId))