Let's say I have a list of objects:
var items = new {
new { Order = 0 },
new { Order = 1 },
new { Order = -1 },
new { Order = 3 },
new { Order = 2 },
new { Order = -1 }
};
I need to order it so that the elements with "Order> -1" are above the list, sorted in ascending order, and the rest of the elements with "Order == -1" will execute them.
Is there a more elegant way to do this than using Conact () + Where () clauses:
var orderedItems = items.Where(x => x.Order > -1).OrderBy(x => x.Order)
.Conact(items.Where(x => x.Order == -1);
So, after sorting, this list will look like this:
var items = new {
new { Order = 0 },
new { Order = 1 },
new { Order = 2 },
new { Order = 3 },
new { Order = -1 },
new { Order = -1 }
};
Also, the "items" list in a real scenario is already a complex IQueryable object. That’s why I’m trying to find the most optimal way to do such selective ordering.
source
share