Generally speaking, you need different queries for this. The same goes for building up different WHERE
or Where(...)
clauses based on user selection:
var query = OrderQuery;
if (userSelection == "GroupByName") {
query = from to in query
group to by to.FromDateUtc into groupedDate
let grouped = groupedDate.GroupBy(o => o.Name);
}
else if (userSelection == "GroupBySomethingElse") {
query = from to in query
group to by to.FromDateUtc into groupedDate
let grouped = groupedDate.GroupBy(o => o.SomethingElse);
}
You'll want to prepare as much common logic as possible before you apply the grouping to query
. Because LINQ uses deferred execution you can modify query
as much as you need before enumerating the result and you will not incur a performance hit.