I have been working on some code that involves using Linq to query a database and came across something that I thought was really useful. So here is the situation:
We have a query that we want to be able to sort in multiple ways. This will allow the user to click on columns and sort by it. That kind of thing. The first way that I did this was to do something like the following:
if (sortmethod.CompareTo("date") == 0)
{
var c = from p in db.ProductSales
from t in db.Products
where (t.Id == p.ProductId)
group t by t into g
orderby g.Count() descending
select g;
} else {
var c = from p in db.ProductSales
from t in db.Products
...
Go to the complete details ...