views:

217

answers:

2

Hi there.

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 string 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?

+2  A: 

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

Robert Harvey
It's seem to me, that I need to create SQL query directly in string :(
AlfeG
Or you can create an `If` tree with every possible permutation of queries. Personally, I prefer the Dynamic approach.
Robert Harvey
A: 

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.

alt text

jinsungy
You linked a picture from the article posted by @Robert.
Nate Bross