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.
Not sure how you're matching, but maybe something like this
from c in aCategories where c.Search.Any(s => s.SearchStandard.Contains("Aston Martin") == true) select new SearchPreviewListCategoriesViewModel { CategoryID = c.Id, Name = c.Name, SearchPreviewControllerAction = c.SearchPreviewControllerAction, SearchCount = (c.Search.Where(s => s.SearchStandard.Contains("Aston Martin") == true)).Count() };
where aCategories is your context access to the Category table.
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:
public class SearchCategoriesListDto
{
public int Id { get; set; }
public string Name { get; set; }
public string SearchPreviewControllerAction { get; set; }
public int SearchCount { get; set; }
}
Step 2:
Refacturing of the mapping between the DTO and the ViewModel in the Presentation Layer:
Mapper.CreateMap<SearchCategoriesListDto, SearchCategoriesListViewModel>();
Step 3:
Method to deliver the CategoryName, the Count by Category and the ControllerAction Name that will deliver the list for each Category list:
public IList<SearchCategoriesListDto> SearchPreviewCategories(String keywords)
{
string keywordsClean = keywords;
keywordsClean = keywordsClean.ToUpper();
keywordsClean = StringUtils.StringSimbolsRemove(keywordsClean, HeelpResources.StringSymbolsToCleanFrom, HeelpResources.StringSymbolsToCleanTo);
string[] splitKeywords = keywordsClean.Split(new Char[] { ' ' });
var searchQuery = _searchRepository.Query;
foreach (string keyword in splitKeywords)
{
searchQuery = searchQuery.Where(p => p.SearchStandard.Contains(keyword));
}
var categoryQuery = _categoryRepository.Query;
var query = from sq in searchQuery
join cq in categoryQuery on sq.CategoryId equals cq.Id
select new SearchCategoriesListDto {
Name = cq.Name,
SearchCount = searchQuery.Where(c => c.CategoryId == cq.Id).Count(),
SearchPreviewControllerAction = cq.SearchPreviewControllerAction
};
var searchResults = query.Distinct().ToList();
return searchResults;
}
Thanks for all the help I got from StackOverlow to solve this challenge.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.