Efcore: Select following GroupBy throws exception for SqLite

Created on 12 Jul 2016  路  3Comments  路  Source: dotnet/efcore

Steps to reproduce

Execute a GroupBy linq statement, followed by a Select results into a Collection throws an exception.
When I add a .ToList() however it works.

public class Set
{
    public int SetId { get; set; }
    public DateTime Date { get; set; }
}

public class SetCollection : ObservableCollection<Set>
{
    public SetCollection(DateTime date, IEnumerable<Set> collection)
        : base(collection)
    {}
}

var sets = new ObservableCollection<SetCollection>
(
    await Db.Sets
        .GroupBy(s => s.Date.Date)
        .Select(c => new SetCollection(c.Key, c)) // <-- throws the exception
//      .Select(c => new SetCollection(c.Key, c.ToList())) // <-- This works
        .ToListAsync()
);

I can provide a mini solution if required.

The issue

Throws an exception. The Select produces a class which provides the IEnumerable interface so I don't understand why it's unable to construct the SetCollection inline.

Exception message:
{"The given expression 'new SetCollection([c].Key, [c])' does not contain the searched expression '[c]' in a nested NewExpression with member assignments or a MemberBindingExpression.\r\nParameter name: fullExpression"}

Stack trace:

at Remotion.Linq.Clauses.ExpressionVisitors.AccessorFindingExpressionVisitor.FindAccessorLambda(Expression searchedExpression, Expression fullExpression, ParameterExpression inputParameter)\r\n   
at Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.<>c__DisplayClass48_0`1.<GetEntityAccessors>b__0(EntityTrackingInfo entityTrackingInfo)\r\n   
at System.Linq.Enumerable.WhereSelectListIterator`2.MoveNext()\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 Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.GetEntityAccessors[TResult](IEnumerable`1 entityTrackingInfos, Expression selector)

Further technical details

EF Core version: "Microsoft.EntityFrameworkCore.Sqlite": "1.0.0"
Operating system: Windows 10
Visual Studio version: VS 2015 update 3

closed-fixed type-bug

All 3 comments

How would you translate new SetCollection(c.Key, c) to SQL?
I guess the first doesn't work because it tries to translate the expression to a SQL.
The latter works because you get the results before passing it to SetCollection constructor.

Not sure what EF team's policy about stuff like this, but I consider it is "by-design".
Interesting to know how they see it.

I wouldn't expect the SetCollection(c.Key, c) to be translated into SQL, unsure why you think it would. I expect this object to be constructed from the results of query.

The difference between the two in my view, is that the first will allow me to construct my ObservableCollection from the IGrouping object (which provides an IEnumerator interface), rather than via a List constructed from the IGrouping.

It's this extra step I'm assuming adds a level of inefficiency.

Verified fixed in 2.0.

Was this page helpful?
0 / 5 - 0 ratings