Can't include objects of nested list with Entity query

entity-framework-core linq

Question

I am using Entity Framework Core and I have the following structure:

class Foo
{
    public ICollection<Bar> Bars { get; set; }
}

class Bar
{
    public XPTO Xpto { get; set; }
}

class XPTO
{
    public string Message { get; set; }
}

How can I include in my Linq query the XPTO Object?

I have tried:

context.Foo.Include(o => o.Bars).ToList(); // This gets me the Bars but the XPTO's of them are null

context.Foo.Include(o => o.Bars).ThenInclude(o => o.Select(x => x.Xpto)).ToList(); This throws an error:

System.ArgumentException: The property expression 'o => {from Bar in o select [x].Xpto}' is not valid. The expression should represent a property access: 't => t.MyProperty'.

What am I missing here?

1
0
5/30/2017 11:04:33 AM

Accepted Answer

Just for someone else facing the same problem using this

 context.Foo.Include(o => o.Bars).ThenInclude(x => x.Xpto).ToList();

you may not get any Intellisense help for that but it would still work with ef core.

2
5/30/2017 11:36:30 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow