0

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

ErrorMessage

"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](IEnumerable1 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.

10
  • ActivityEntry doesn't seem to contain an Employee property. I doubt that's the problem, but it makes me wonder what else you're not showing... :-) Commented Jun 24, 2016 at 15:22
  • You would generally get no error when constructing the query above - if an error is to be generated, then usually that'll happen when you execute it, for example by calling ToList(). The specific exception quoted is one you'd likely get from Single(). Can you show us how the results are materialized (i.e. how the results of the query are used)? Commented Jun 24, 2016 at 15:24
  • @Gary McGill Added the employee (I try to include only the parts necessary to understand the question when I post here on SO ;-)) + Added ToList() - missed that - the exception is thrown when executing. Commented Jun 24, 2016 at 15:34
  • Can you show how the filter variable is declared and initialized, and a bit more of the stack trace? I'm still skeptical that ToList() would give that error rather than Single() or SingleOrDefault() Commented Jun 24, 2016 at 15:51
  • 1
    The reason it works that way round is that it's loading the entire table into memory and applying the Where clause there. That would be a bad thing to do :) Commented Jun 24, 2016 at 16:07

1 Answer 1

2

This was an error in RC1 of EF7, fixed in RC2.

=> https://github.com/aspnet/EntityFramework/issues/4394

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.