1

I'm using entity framework core. I'm attempting to create a list of products and include associate reviews and review author data.

I have a 3 entities:

public class Product
{
    [Key]
    public int ID { get; set; }
    public ICollection<Review> Reviews {get; set;}
}

public class Review
{
        [Key]
        public int ID { get; set; }
        public Product Product {get; set;}
        public Customer Author { get; set; }
}

public class Customer
{
        [Key]
        public int ID { get; set; }
        public ICollection<Review> Reviews { get; set; }
}

When, refering to this answer, I request a list of products with reviews and authors:

context.Products.Include(p=> p.Reviews.Select(r => r.Author)).ToList();

The following error is thrown:

System.InvalidOperationException
Message = The property expression 'p => {from Review r in [p].Reviews select [r].Author}' is not valid. The expression should represent a property access: 't => t.MyProperty'

Any advice would be greatly appreciated.

Community
  • 1
  • 1
Stephen Paul
  • 37,253
  • 15
  • 92
  • 74

1 Answers1

1

It seem to be something still not done or missing, but anyway you can do it like that:

context.Products.Include(p => p.Reviews).ThenInclude(x=>x.Author).ToList();
Bassam Alugili
  • 16,345
  • 7
  • 52
  • 70
  • Thanks @Bassam :) However, in your example, the type of 'x' is ICollection, not Review. So I don't think this works. – Stephen Paul Jul 27 '16 at 17:26
  • @StevePaul this is exaclty like your query and it is working! just try it^^ – Bassam Alugili Jul 27 '16 at 17:33
  • 1
    My apoligies - you are right and I am wrong! For some reason, it was throwing compilation errors. I recompiled my model classes and the errors dissappeared! I'm very confused. But thank you for your time :) – Stephen Paul Jul 27 '16 at 18:06