LINQ 2 SQL Group by multiple custom columns
Is there a way to create LINQ query that will have different grouping fields. I.E. I have a class
public class Stat
{
public DateTime Date { get; set; }
public int ApplicationId { get; set; }
public int ActionType { get; set; }
public stri开发者_开发技巧ng Version { get; set; }
}
Now I need to query table with this data with grouping by several custom fields. For example, group by ApplicationId and ActionType or by Application and Version
I don't want to write different queries for all possible combinations of fields. And want to make a generic method, that will accept list on column names where grouping must occur.
So is there a way to create such a query in runtime?
You can do it with Dynamic Linq:
http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx
By using the LINQ Dynamic Query Library, you can express LINQ queries using extension methods (for example ".Where and .GroupBy") that take string arguments instead of type-safe language operators.
(source: scottgu.com)
精彩评论