【发布时间】:2015-09-06 17:52:54
【问题描述】:
以下是相关代码的 sn-p。我们正在尝试将一个项目与项目属性的集合进行匹配。下面的代码有效,我们返回了我们需要的东西,但是它很慢。我们很好奇这样做的最佳方法是什么。任何帮助是极大的赞赏。我们也对 SQL 中的选项持开放态度。谢谢!
var items = await GetItemsAsync(repository, a, b);
// Next we need to get all of the properties for those items.
var results = new List<Result>();
foreach(var c in items)
{
c.Properties = await GetItemProperties(repository, c.Id);
var matchedProperties = selectedProperties.Where(si => c.Properties.Any(i => i.Id == si.Id));
if(matchedProperties.Count() > 0)
{
results.Add(new Result(c)
{
MatchedProperties = matchedProperties,
Relevance = (decimal)matchedProperties.Count() / (decimal)selectedProperties.Count()
});
}
}
【问题讨论】:
-
您的代码的哪一部分非常慢?GetItemsAsync 或 GetItemProperties 或在 selectedProperties 中的什么位置。因为我们不知道这些方法中的代码是什么。很难提供帮助。我唯一能说的是只需 int selectedProperties.Count() 转换十进制一次。
-
或许可以考虑使用位掩码。
标签: c# sql model-view-controller