I\'m creating a mock data source that I want to be able to pass in a list of SortExpressions on.
public SortExpression(string name, SortDirection direction)
There are two problems. The first is the one others have alluded to - you need to use the value returned by OrderBy
etc. The second is that each time you call OrderBy
, that's adding a new "primary" ordering. You really want ThenBy
after the first ordering has been applied. That makes it pretty ugly, unfortunately. It's still pretty ugly after a refactoring, but not too bad...
IEnumerable<Data> query = from item in items select item;
if (sortExpressionsExist)
{
// Won't be read in the first iteration; will be written to
IOrderedEnumerable<Data> orderedQuery = null;
for (int i = 0; i < sortExpressions.Count; i++)
{
// Avoid single variable being captured: capture one per iteration.
// Evil bug which would be really hard to find :)
int copyOfI = i;
// Tailor "object" depending on what GetProperty returns.
Func<Data, object> expression = item =>
item.GetType()
.GetProperty(sortExpressions[copyOfI].Name)
.GetValue(item, null);
if (sortExpressions[i].Direction == SortDirection.Ascending)
{
orderedQuery = (i == 0) ? query.OrderBy(expression)
: orderedQuery.ThenBy(expression);
}
else
{
orderedQuery = (i == 0) ? query.OrderByDescending(expression)
: orderedQuery.ThenByDescending(expression);
}
}
query = orderedQuery;
}
The query is not mutable, so OrderBy returns a new object. You need to make the same call, but add "query =" to the beginning.
query = query.OrderBy(item => item.GetType().GetProperty(sortExpressions[i].Name));
This will work:
YourCollection.Orderby(item => item.Property1).ThenBy(item => item.Property2);
The OrderBy/OrderByDescending 'operators' work like String.ToUpper(), i.e., they take the thing you invoke it on, and yield a 'copy' that has what you asked for.
In other words, instead of saying:
query.Orderby(item->item.X)
you should do
query = query.Orderby(item->item.X)
or
sortedResult = query.Orderby(item->item.X)
[And as Jon Skeet points out, use ThenBy
/ThenByDescending
as in his answer]
OrderBy on IEnumerable returns returns an IOrderedEnumerable. It does not sort them in line. So get the return value from your .OrderBy and you will be fine.
OrderBy returns a new IEnumerable, so you need to do something like:
IEnumerable<Data> results
= query.OrderBy(item => item.GetType().GetProperty(sortExpressions[i].Name));