score:2

Accepted answer

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.

score:0

If you are trying to do something like this

SELECT * FROM <TableName> GROUP BY <Column1>,<Column2>

Then, take a look at this post

Group By Multiple Columns


Related Articles