Mujangi Bia
Mujangi Bia

Reputation: 35

Order a list with unique rows with a given row as first item, then reorder the list but without the first row

let's say i have a list with this records in ("Germany", "Belgium", "Netherlands", "Spain", "Russia", "Italy").

And i want to order this list with "Belgium" on top of the list.

So that when i load this list into a ComboBox "Belgium" is the selectedvalue by load.

Now how do i order the rest of the list?

This is what i have: "Belgium" comes at the top but the rest is not ordered.

 return Enumerable.Select(GetTable()
            .OrderByDescending(o => o.Name == country)
            .ThenBy(o => o.Name != country), b =>
                new ComboBoxBase.ComboBoxListStructGuid { Id = b.CountryID, Description = b.Country }).ToList();

Upvotes: 2

Views: 47

Answers (2)

Robert McKee
Robert McKee

Reputation: 21477

return Enumerable.Select(GetTable()
            .OrderByDescending(o => o.Name == country)
            .ThenBy(o => o.Name), b =>
                new ComboBoxBase.ComboBoxListStructGuid { Id = b.CountryID, Description = b.Country }).ToList();

Upvotes: 1

Magnus
Magnus

Reputation: 46947

var countries = new List<string>(){ "Germany", "Belgium", "Netherlands", "Spain", "Russia", "Italy" };
var result = countries
                .OrderByDescending (c => c == "Belgium")
                .ThenBy (c => c);

Upvotes: 4

Related Questions