ASP.NET MVC2字典未使用动态索引进行更新

时间:2011-01-11 02:10:54

标签: linq-to-sql asp.net-mvc-2

我正在开发一种搜索引擎,可以参数化地对数据库项进行评分。因此,我设置了一个评分类,允许控制器管理和返回特定项目的分数。例如,假设我正在为计算机评分。如果有人在搜索表单中选择了特定数量的RAM,我希望匹配得分高于近似匹配。

该类允许基于数据库项的ID作为索引添加分数:

public class ScoringMechanism {
    // Dictionary that holds scores
    private Dictionary<int, int> _scores;

    public ScoringMechanism() {
        this._scores = new Dictionary<int, int>();
    }

    public AddOrUpdate(int id, int score) {
        if (_scores.ContainsKey(id)) {
            _scores[id] += score;
        } else {
            _scores.Add(id, score);
        }
    }
}

在控制器中,我检查匹配是这样的:

var computers = from computer in _context.Computers select computers;

foreach (Computer computer in computers) {
    if (computer.GBAmt == Request.Querystring["GB"]) {
       scores.AddOrUpdate(computer.ID, 15);
    }
}

我在代码中注意到的是我无法弄清楚:当通过computer.ID变量动态更新分数时,分数不会更新。但是,当我明确声明一个id时,它会更新。

实质上,

scores.AddOrUpdate(15, 25);
有效。

0 个答案:

没有答案