I'm using Entity Framework core. I have the following entities:
public class Category {
public long Id { get; set; }
public string Name { get; set; }
}
public class Product {
public long Id { get; set; }
public long CategoryId { get; set; }
public string Name { get; set; }
}
I'd like to get a grouping whereby my key is my Category and I have a list of Products. I'd like to do this running a single query against the DB.
var data = context.Categories
.Join(
Products,
c => c.Id,
p => p.CategoryId,
(c, p) => new {
Category = c,
Product = p
}
)
.ToList();
This runs the query I want and seems to produce a list with an anonymous object that has a Category and Product. If I then do the following, it kind of does what I want:
var grouped = data.GroupBy(x => new { x.Category });
The Key is fine, but the list of values seems to repeat the Categories. Is there a way to make it so I have a Key that's the Category and then the values is a list of Product objects? Would prefer method syntax, but at this point I can hopefully figure out query syntax if that's what somebody can get me.