I may be way off, but some googling has led me to produce this query:
VMCategory = from pc in _context.ProductCategories
join pic in _context.ProductsInCategories
on pc.Id equals pic.ProductCategoryId
group pic by pc into x
select new ViewModelProductCategory
{
Id = x.Key.Id,
ParentId = x.Key.ParentId,
Title = x.Key.Title,
SortOrder = x.Key.SortOrder,
NumOfProductsInThisCategory = x.Count(c => [SOMETHING IS MISSING])
}).
ToList();
I'm trying to populate the viewmodel list with the category items in the entity model and a count of all the products in each category.
I need this type of result (as items in a list of category objects):
Id = 6 (from ProductCategory)
ParentId = 4 (from ProductCategory)
Title = "Example" (from ProductCategory)
SortOrder = 2 (from ProductCategory)
NumOfProductsInThisCategory = 7 (count products from ProductsInCategories)
These are my models:
Viewmodel:
public class ViewModelProductCategory
{
public int Id { get; set; }
public int? ParentId { get; set; }
public string Title { get; set; }
public int SortOrder { get; set; }
public int NumOfProductsInThisCategory { get; set; }
}
Entity models:
public class ProductCategory
{
public int Id { get; set; }
public int? ParentId { get; set; }
public string Title { get; set; }
public int SortOrder { get; set; }
}
public class ProductInCategory
{
public int Id { get; set; }
public int ProductId { get; set; }
public int ProductCategoryId { get; set; }
public int SortOrder { get; set; }
}