[英]MVC Entity Framework Count
I would like to build a result like: CategoryID || 我想建立一个类似的结果: CategoryID || Count(Searches) ||
计数(搜索)|| CategoryName based on two Entity Framework based classes:
CategoryName基于两个基于Entity Framework的类:
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". 我已经做了一个SearchPreview方法,该方法基于“ Aston Martin”之类的搜索返回搜索结果,该搜索将在所有类别中都使用单词“ 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" 现在的挑战是构建一种方法,该方法在搜索关键字“阿斯顿·马丁”之后返回,其中的类别中包含那些包含计数在内的关键字,例如“自动:2 |沸点:3 |商店: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. 我试图避免对每个类别进行查询以计算“搜索”条目的数量,并且我想找到一个GroupBy解决方案,该解决方案采用已经执行的SearchPreview方法并提取其中包含的GroupBy类别。
I am using the ViewModel SearchPreviewListCategoriesViewModel
to map from the models using Automapper: 我正在使用ViewModel
SearchPreviewListCategoriesViewModel
使用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. 希望somoeone可以帮助我。
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. 其中aCategories是您对Category表的上下文访问。
Solution found to the problem: 找到问题的解决方案:
Step 1: 第1步:
Creation of a Data Transfer Object to take the Domain object and deliver it to the Presentation Web Layer: 创建一个数据传输对象以获取Domain对象并将其传递到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: 第2步:
Refacturing of the mapping between the DTO and the ViewModel in the Presentation Layer: 在表示层中重新构造DTO和ViewModel之间的映射:
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: 传递CategoryName,按类别计数和ControllerAction名称的方法,该方法将传递每个Category列表的列表:
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. 感谢您从StackOverlow获得的所有帮助来解决此难题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.