LINQ - Sorting a list of objects using OrderBy EnumValue and a collection of join results?

Hello! I want to use LINQ to query a list of objects and sort them by enumeration value, a string value, and then combining the rest of the list, sorted in a similar way, LINQ's newbie is here, so be careful.

//CTOR for my class
MyClass(id, "Name Value Here", Enum.EnumValueHere);

I create a list of these objects and want to sort them differently so that all elements with Enum.EnumValue [X] are shown first in a list sorted by name, and then all other elements sorted in a similar way (almnost as a union of result sets in SQL)

//What I have so far (only sorts the list)
List<MyClass> = (from s in MyClass orderby s.EnumValue, s.NameValue  select s).ToList();

Do any gurus have any kind of LINQ Magic to share?

Thanks!

+3
2

, , - ?

// Using Color as enum just to make things clearer
class ClassyClass
{
    public string Name {get; private set;}
    public Color Color {get; private set;}
    public ClassyClass(id, string name, Color color) { ... }
}


var orderedList = collectionOfClassyClasses
            .OrderBy(x => x.Color != Color.Red) // False comes before true
            .ThenBy(x => x.Color)
            .ThenBy(x => x.Name)
            .ToList();

collectionOfClassyClasses ClassyClass, orderedList , , , , , , . , , ... : p , , -.

+10

list.AddRange(), ienumerable.

0

Source: https://habr.com/ru/post/1712676/


All Articles