I have a reporting interface where the end user gets to select multiple fields for the sort order of the returned report. The problem I am having is that I can't really chain the OrderBy / ThenBy methods, since I'm iterating through a list of sort fields. I'm thinking something like this:
foreach (string sort in data.SortParams)
{
switch (sort)
{
case "state":
query = query.ThenBy(l => l.RegionCode);
break;
case "type":
query = query.ThenBy(l => l.Type);
break;
case "color":
query = query.ThenBy(l => l.Color);
break;
case "category":
query = query.OrderBy(l => l.Category);
break;
}
}
(Note: I've removed the switch determining if this is the first sort item for simplicity's sake.)
Any thoughts on how to iterate through a collection to determine the sort order?
You could do what you want if you use an initial "seed" OrderBy:
EDIT you need to call OrderBy
to create an IOrderedEnumerable
(or IOrderedQueryable
) first before attaching ThenBy
clauses:
var orderedQuery = query.OrderBy(l => 0);
foreach (string sort in data.SortParams)
{
switch (sort)
{
case "state":
orderedQuery = orderedQuery.ThenBy(l => l.RegionCode);
break;
case "type":
orderedQuery = orderedQuery.ThenBy(l => l.Type);
break;
case "color":
orderedQuery = orderedQuery.ThenBy(l => l.Color);
break;
case "category":
orderedQuery = orderedQuery.ThenBy(l => l.Category);
break;
}
}
query = orderedQuery; // cast back to original type.
If you want something more flexible check out this answer