I would like to build a result like: CategoryID || Count(Searches) || CategoryName based on two Entity Framework based classes:
public class Category
{
// Primary properties
public int Id { get; set; }
public string Name { get; set; }
public string SearchPreviewControllerAction { get; set; }
}
public class Search
{
// Primary properties
public int Id { get; set; }
public string SearchTitle { get; set; }
public string SearchStandard { get; set; }
public int CategoryId { get; set; }
// Navigation properties
public virtual Category Category { get; set; }
}
I already made a SearchPreview method that returns the Searches based on a search like “Aston Martin” that will find in all categories with the words “Aston Martin”.
The challenge now is to build a method that returns, after the Search of the keywords “Aston Martin”, in which categories there are those keywords including the count, like “Auto: 2 | Seeling point: 3 | Shop: 2”
I am trying to avoid a query for each category to count the number of Searches entry, and I would like to find a GroupBy solution, that takes the SearchPreview method already executed and extract the GroupBy Categories that it contains.
I am using the ViewModel SearchPreviewListCategoriesViewModel to map from the models using Automapper:
public class SearchPreviewListCategoriesViewModel
{
public int CategoryID { get; set; }
public string Name { get; set; }
public string SearchPreviewControllerAction { get; set; }
public int SearchCount { get; set; }
}
Hope somoeone can help me on this.
Thank you.
Regards,
Patrick.
Solution found to the problem:
Step 1:
Creation of a Data Transfer Object to take the Domain object and deliver it to the Presentation Web Layer:
Step 2:
Refacturing of the mapping between the DTO and the ViewModel in the Presentation Layer:
Step 3:
Method to deliver the CategoryName, the Count by Category and the ControllerAction Name that will deliver the list for each Category list:
}
Thanks for all the help I got from StackOverlow to solve this challenge.