Страницы

Поиск по вопросам

суббота, 4 января 2020 г.

LINQ сортировка по динамическим параметрам

#c_sharp #net #linq


Прошу помощи в оптимизации 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();

    


Ответы

Ответ 1



Попробуйте что-то в таком стиле: 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();

Ответ 2



Вариант с TechDataComparer для List public class ListTechDataComparer : IComparer> { private IComparer comparer = new TechDataComparer(); public int Compare(List x, List y) { for (int i = 0; i < x.Count; i++) { var compareResult = comparer.Compare(x[i],y[i]); if (compareResult != 0) return compareResult; } return 0; } } и использовать newItemsReportInfo = itemsReportInfo.OrderBy(x => x.FlyerPos) .ThenBy(x => x.SortingTH, new ListTechDataComparer()) .ThenBy(x => x.Description) .ThenBy(x => x.Flieztext) .ToList();

Комментариев нет:

Отправить комментарий