Прошу помощи в оптимизации LINQ сортировки. Код написанный ниже, был написан по принципу "ну ведь так же работает?". Но он не красив, не гибок, и хотелось бы понять, как можно и можно ли через линкью, в качестве параметра для сортировки, указывать List.
var newItemsReportInfo = new List();
switch (itemsReportInfo[0].SortingTH.Count)
{
case 0:
newItemsReportInfo = itemsReportInfo.OrderBy(x => x.FlyerPos)
.ThenBy(x => x.Description)
.ThenBy(x => x.Flieztext)
.ToList();
break;
case 1:
newItemsReportInfo = itemsReportInfo.OrderBy(x => x.FlyerPos)
.ThenBy(x => x.SortingTH[0], new TechDataComparer())
.ThenBy(x => x.Description)
.ThenBy(x => x.Flieztext)
.ToList();
break;
case 2:
newItemsReportInfo = itemsReportInfo.OrderBy(x => x.FlyerPos)
.ThenBy(x => x.SortingTH[0], new TechDataComparer())
.ThenBy(c => c.SortingTH[1], new TechDataComparer())
.ThenBy(x => x.Description)
.ThenBy(x => x.Flieztext)
.ToList();
break;
case 3:
newItemsReportInfo = itemsReportInfo.OrderBy(x => x.FlyerPos)
.ThenBy(x => x.SortingTH[0], new TechDataComparer())
.ThenBy(c => c.SortingTH[1], new TechDataComparer())
.ThenBy(c => c.SortingTH[2], new TechDataComparer())
.ThenBy(x => x.Description)
.ThenBy(x => x.Flieztext)
.ToList();
break;
case 4:
newItemsReportInfo = itemsReportInfo.OrderBy(x => x.FlyerPos)
.ThenBy(x => x.SortingTH[0], new TechDataComparer())
.ThenBy(c => c.SortingTH[1], new TechDataComparer())
.ThenBy(c => c.SortingTH[2], new TechDataComparer())
.ThenBy(c => c.SortingTH[3], new TechDataComparer())
.ThenBy(x => x.Description)
.ThenBy(x => x.Flieztext)
.ToList();
break;
case 5:
newItemsReportInfo = itemsReportInfo.OrderBy(x => x.FlyerPos)
.ThenBy(x => x.SortingTH[0], new TechDataComparer())
.ThenBy(c => c.SortingTH[1], new TechDataComparer())
.ThenBy(c => c.SortingTH[2], new TechDataComparer())
.ThenBy(c => c.SortingTH[3], new TechDataComparer())
.ThenBy(c => c.SortingTH[4], new TechDataComparer())
.ThenBy(x => x.Description)
.ThenBy(x => x.Flieztext)
.ToList();
break;
case 6:
newItemsReportInfo = itemsReportInfo.OrderBy(x => x.FlyerPos)
.ThenBy(x => x.SortingTH[0], new TechDataComparer())
.ThenBy(c => c.SortingTH[1], new TechDataComparer())
.ThenBy(c => c.SortingTH[2], new TechDataComparer())
.ThenBy(c => c.SortingTH[3], new TechDataComparer())
.ThenBy(c => c.SortingTH[4], new TechDataComparer())
.ThenBy(c => c.SortingTH[5], new TechDataComparer())
.ThenBy(x => x.Description)
.ThenBy(x => x.Flieztext)
.ToList();
break;
default:
newItemsReportInfo = itemsReportInfo.OrderBy(x => x.FlyerPos)
.ThenBy(x => x.SortingTH[0], new TechDataComparer())
.ThenBy(c => c.SortingTH[1], new TechDataComparer())
.ThenBy(c => c.SortingTH[2], new TechDataComparer())
.ThenBy(c => c.SortingTH[3], new TechDataComparer())
.ThenBy(c => c.SortingTH[4], new TechDataComparer())
.ThenBy(c => c.SortingTH[5], new TechDataComparer())
.ThenBy(c => c.SortingTH[6], new TechDataComparer())
.ThenBy(x => x.Description)
.ThenBy(x => x.Flieztext)
.ToList();
break;
}
Как видно из кода, мы всегда смотрим на размер itemsReportInfo[0].SortingTH и далее уже применяем один из методов сортировки. Хотелось бы заменить этот код, на код вида.
newItemsReportInfo = itemsReportInfo.OrderBy(x => x.FlyerPos)
.ThenBy(x => x.SortingTH, new TechDataComparer())
.ThenBy(x => x.Description)
.ThenBy(x => x.Flieztext)
.ToList();
Ответ
Попробуйте что-то в таком стиле:
var newItemsReportInfo = new List();
var query = itemsReportInfo.OrderBy(x => x.FlyerPos);
for (int i = 0; i < itemsReportInfo[0].SortingTH.Count; i++)
{
query = query.ThenBy(x => x.SortingTH[i], new TechDataComparer());
}
query = query.ThenBy(x => x.Description);
query = query.ThenBy(x => x.Flieztext);
newItemsReportInfo = query.ToList();
Комментариев нет:
Отправить комментарий