I have the following classes:
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; }
}
I want to query all ActivityEntries where the CostCenter has certain ActivityIds.
For the filter I have a List with activityIds (filterList). The following query throws an System.InvalidOperationException
"The Sequence contains more than one element"
And I do not understand why.
var filterList = new List<int> {1};
var data = _db.ActivityEntry
.Include(x => x.CostCenter)
.Where(x => filterList.Contains(x.CostCenter.ActivityId))
.ToList();
Another WHERE statement which doesn't use the nested object CostCenter is working fine.
.Where(x =>filterList.Contains(x.CostCenterId))
Do I need to use another syntax to query for the activity-Ids in CostCenter object?
Stack Trace:
bei System.Linq.Enumerable.Single[TSource](IEnumerable
1 source) bei Microsoft.Data.Entity.Query.EntityQueryModelVisitor.<>c__DisplayClass79_01.b__0(IEnumerable1 ps, IQuerySource qs) bei Microsoft.Data.Entity.Query.EntityQueryModelVisitor.BindMemberExpressionCore[TResult](MemberExpression memberExpression, IQuerySource querySource, Func3 memberBinder) bei Microsoft.Data.Entity.Query.EntityQueryModelVisitor.BindMemberExpression[TResult](MemberExpression memberExpression, IQuerySource querySource, Func3 memberBinder) bei Microsoft.Data.Entity.Query.EntityQueryModelVisitor.BindMemberExpression(MemberExpression memberExpression, Action2 memberBinder) bei Microsoft.Data.Entity.Query.ExpressionVisitors.Internal.RequiresMaterializationExpressionVisitor.VisitMember(MemberExpression memberExpression) bei System.Linq.Expressions.MemberExpression.Accept(ExpressionVisitor visitor) bei Microsoft.Data.Entity.Query.ExpressionVisitors.ExpressionVisitorBase.Visit(Expression expression) bei Remotion.Linq.Clauses.ResultOperators.ContainsResultOperator.TransformExpressions(Func2 transformation) bei Remotion.Linq.QueryModel.TransformExpressions(Func2 transformation) bei Microsoft.Data.Entity.Query.ExpressionVisitors.Internal.RequiresMaterializationExpressionVisitor.VisitSubQuery(SubQueryExpression subQueryExpression) bei Remotion.Linq.Clauses.Expressions.SubQueryExpression.Accept(ExpressionVisitor visitor) bei Microsoft.Data.Entity.Query.ExpressionVisitors.ExpressionVisitorBase.Visit(Expression expression) bei Remotion.Linq.Clauses.WhereClause.TransformExpressions(Func2 transformation) bei Remotion.Linq.QueryModel.TransformExpressions(Func2 transformation) bei Microsoft.Data.Entity.Query.ExpressionVisitors.Internal.RequiresMaterializationExpressionVisitor.FindQuerySourcesRequiringMaterialization(QueryModel queryModel) bei Microsoft.Data.Entity.Query.QueryCompilationContext.FindQuerySourcesRequiringMaterialization(EntityQueryModelVisitor queryModelVisitor, QueryModel queryModel) bei Microsoft.Data.Entity.Query.EntityQueryModelVisitor.CreateQueryExecutor[TResult](QueryModel queryModel) bei Microsoft.Data.Entity.Storage.Database.CompileQuery[TResult](QueryModel queryModel) --- Ende der Stapelüberwachung vom vorhergehenden Ort, an dem die Ausnahme ausgelöst wurde --- bei Microsoft.Data.Entity.Query.Internal.QueryCompiler.<>c__DisplayClass18_01.<CompileQuery>b__0() bei Microsoft.Data.Entity.Query.Internal.CompiledQueryCache.GetOrAddQuery[TResult](Object cacheKey, Func1 compiler) bei Microsoft.Data.Entity.Query.Internal.QueryCompiler.CompileQuery[TResult](Expression query) bei Microsoft.Data.Entity.Query.Internal.QueryCompiler.Execute[TResult](Expression query) bei Microsoft.Data.Entity.Query.Internal.EntityQueryProvider.Execute[TResult](Expression expression) bei Remotion.Linq.QueryableBase1.GetEnumerator() bei System.Collections.Generic.List1..ctor(IEnumerable1 collection) bei System.Linq.Enumerable.ToList[TSource](IEnumerable1 source) bei zetvnext.application.Services.ReportService.GetCostCenterReportData(FilterDto filter) in C:\Source\zetvnext\src\zetvnext.application\Services\ReportService.cs:Zeile 86. bei zetvnext.tests.ReportServiceTests.Test_Report_Data() in C:\Source\zetvnext\tests\zetvnext.tests\ReportServiceTests.cs:Zeile 142.

ToList(). The specific exception quoted is one you'd likely get fromSingle(). Can you show us how the results are materialized (i.e. how the results of the query are used)?filtervariable is declared and initialized, and a bit more of the stack trace? I'm still skeptical thatToList()would give that error rather thanSingle()orSingleOrDefault()Whereclause there. That would be a bad thing to do :)