efcore icon indicating copy to clipboard operation
efcore copied to clipboard

Cannot select IEnumerable object in ThenInclude

Open Cowlephant opened this issue 10 years ago • 6 comments

Steps to reproduce

var entriesResult = platformContext.GoalEntries
                .Include(e => e.GoalUsers)
                .Include(e => e.Milestone)
                .Include(e => e.AggregateCollection)
                    .ThenInclude(e => e.Category)
                .Include(e => e.AggregateCollection)
                    .ThenInclude(e => e.AggregateCollectionAggregates)
                    .ThenInclude(ac => ac.Select(aca => aca.Aggregate))
                .Include(e => e.Metric)
                .Select(e => e).ToList();

The issue

I am trying to select a nested object with ThenInclude that is an ICollection, but Entity Framework doesn't seem to like what I am trying to accomplish. I've tried writing it in numerous different ways as well... with just an include that chains down to the object I'm looking for, fewer ThenIncludes, etc. Am I doing this wrong? Is this a bug or a limitation?

The error happens on: .ThenInclude(ac => ac.Select(aca => aca.Aggregate))

This is the exception I'm getting:

If you are seeing an exception, include the full exceptions details (message and stack trace).

The property expression 'ac => {from AggregateCollectionAggregate aca in ac select [aca].Aggregate}' is not valid. The expression should represent a property access: 't => t.MyProperty'. For more information on including related data, see http://go.microsoft.com/fwlink/?LinkID=746393.

Further technical details

EF Core version: 1.0.0 Operating system: Windows 7 64bit Visual Studio version: VS 2015

Cowlephant avatar Jul 26 '16 15:07 Cowlephant

You shouldn't need to use the Select syntax with EF Core (in fact it's not supported). Visual Studio IntelliSense gets confused with Include/ThenInclude (we're getting that fixed) but you should just be able to do a ThenInclude for the related property without using Select.

rowanmiller avatar Jul 26 '16 16:07 rowanmiller

@rowanmiller I'm not entirely sure what you mean, because even without using the Select it still fails to work.

This doesn't throw an exception:

.Include(e => e.AggregateCollection)
                    .ThenInclude(e => e.AggregateCollectionAggregates)

But it's an IEnumerable join table, and the Aggregate object I'm trying to access in that join table is null.

image

Cowlephant avatar Jul 26 '16 17:07 Cowlephant

@twilliamsgsnetx you should be able to write this:

                .Include(e => e.AggregateCollection)
                    .ThenInclude(e => e.AggregateCollectionAggregates)
                    .ThenInclude(ac => ac.Aggregate)

rowanmiller avatar Jul 26 '16 17:07 rowanmiller

Well that's interesting. My Intellisense failed me here then? All these years working with .NET and I had no idea you could just select a property of the underlying type... even though it doesn't show up via Intellisense.

image

I don't know whether to feel stupid or not.

Time to go back and reevaluate all my other code then...

Cowlephant avatar Jul 26 '16 17:07 Cowlephant

I don't know whether to feel stupid or not.

Definitely don't feel stupid 😄... it's really unintuitive that something wouldn't show up in IntelliSense but would compile. Include/ThenInclude are just pretty complicated for IntelliSense to pick the right overload etc., so we hit an issue that they weren't aware of (we've passed it on for them to fix).

rowanmiller avatar Jul 26 '16 17:07 rowanmiller

@twilliamsgsnetx In case you are curious or you want to provide additional feedback on the Intellisense issue, you can find it at https://github.com/dotnet/roslyn/issues/8237.

divega avatar Jul 26 '16 17:07 divega