我正在开发一个用户可以按类别过滤记录的应用程序。
一条记录必须有 1 个,但可以有多个类别。(1-*)
我的问题是,我能做些什么来改进这个搜索?由于以下原因,它目前以大约 O(n^3) 的速度运行:
一个事务可能有很多记录 (1-many) 一个记录可能有很多类别 (1-many) 遍历选择用于搜索的类别
我正在做的基本大纲如下:
retrieve all announcements
if(startDate and endDate have values)
model = model.Where(x => x.WADate >= start.Value && x.WADate <= end.Value).ToList();
if(queryString is not null)
model = model.Where(
x => x.WANum.Contains(query) ||
x.Tooltip.ToLower(CultureInfo.InvariantCulture).Contains(query)
|| x.Topic.ToLower(CultureInfo.InvariantCulture).Contains(query)
).ToList();
if (selectedCategories.Count > 0)
{
bool HasMatch;
foreach (var ancmt in announcements)
{
HasMatch = false;
foreach (var cat in selectedCategories)
{
foreach (var xref in ancmt.waXref)
{
if (cat.ID == xref.WACategoryID)
{
HasMatch = true;
}
}
}
if(HasMatch)
{
model.Add(new EditViewModel
{
WATypeID = ancmt.WATypeID,
WANum = ancmt.WANum,
WATypeName = ancmt.waType.WATypeDescription,
Link = ancmt.Link,
Tooltip = ancmt.Tooltip,
Topic = ancmt.Topic,
WADate = ancmt.WADate,
WAID = ancmt.WAID,
});
ancmt.waXref.ToList().ForEach(
x => model.Last().Categories.Add(
new CategoryViewModel { ID = x.WACategoryID, Name = x.waCategory.WACategory, IsSelected = false }));
}
}
}
// If no catgories were selected, keep all announcements for next stage of search
else
{
foreach (var ancmt in announcements)
{
model.Add(new EditViewModel
{
WATypeID = ancmt.WATypeID,
WANum = ancmt.WANum,
WATypeName = ancmt.waType.WATypeDescription,
Link = ancmt.Link,
Tooltip = ancmt.Tooltip,
Topic = ancmt.Topic,
WADate = ancmt.WADate,
WAID = ancmt.WAID,
});
ancmt.waXref.ToList().ForEach(
x => model.Last().Categories.Add(
new CategoryViewModel { ID = x.WACategoryID, Name = x.waCategory.WACategory, IsSelected = false }));
}
}
我正在使用方法语法,而不是查询,并且更喜欢留在方法语法中。