我有一个带有 OrderBy 和 ThenBy 的 linq 查询(与数据库无关)
var sortedList = unsortedList
.OrderBy(foo => foo.Bar) //this property access is relatively fast
.ThenBy(foo => foo.GetCurrentValue()) //this method execution is slow
获取foo.Bar
速度很快,但执行foo.GetCurrentValue()
速度很慢。仅当某些成员具有相等的 Bar 值时,返回值才重要,这种情况很少发生,但在发生这种情况时需要考虑。是否可以选择仅在 Bar 值相等的情况下需要平局时才执行 ThenBy 子句?(即如果 foo.Bar 值是唯一的,则不会执行)。
另外,实际上 Bar 也有点慢,所以最好不要为同一个对象调用两次。