Код: Выделить всё
using System;
using System.Linq;
class Program
{
static void Main()
{
var data = new[]
{
new { Name = "Alice", Age = 30 },
new { Name = "Bob", Age = 25 },
new { Name = "Charlie", Age = 25 }
};
var sorted = data
.OrderBy(x => Tracer("First criterion", x.Age))
.ThenBy(x => Tracer("Second criterion", x.Name));
foreach (var item in sorted)
{
Console.WriteLine($"{item.Name} - {item.Age}");
}
}
static T Tracer(string label, T value)
{
Console.WriteLine($"Computing {label} key: {value}");
return value;
}
}
// Output displayed on the screen:
// Computing First criterion key: 30
// Computing First criterion key: 25
// Computing First criterion key: 25
// Computing Second criterion key: Alice
// Computing Second criterion key: Bob
// Computing Second criterion key: Charlie
// Bob - 25
// Charlie - 25
// Alice - 30
Код: Выделить всё
ThenByДругими словами, ключи сортировки всех предложений thenBy вычисляются для всей коллекции.
Есть ли способ обойти эту проблему?>
Подробнее здесь: https://stackoverflow.com/questions/798 ... lowed-by-m
Мобильная версия