优化LINQ OrderByDescending查询

时间:2010-08-16 10:38:20

标签: c# linq

我需要在泛型集合上运行LINQ 当前查询的运行速度非常慢......

var patternsWithMaxLenArray = totalPatterns
    .Where
    (
        pattern => pattern.Contains(maxLen) &&
                   pattern.All(lenInPattern => curQuantities.Keys.Contains(lenInPattern)) &&
                   pattern.GroupBy(len => len).All(g => g.Count() <= curQuantities[g.Key])
    )
    .Select(pattern => new { pattern = pattern, sum = pattern.Sum() })
    .ToArray();

var patternsWithMaxLen = patternsWithMaxLenArray
    .OrderByDescending(patternsum => patternsum.sum)
    .Select(patternsum => patternsum.pattern);

var iq = patternsWithMaxLen.First();

1 个答案:

答案 0 :(得分:2)

测量测量措施。将所有内容删除到最简单的查询并进行测量。然后一次添加一下,直到找到“洞”。