Click here to Skip to main content
15,400,137 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have datatable with 3 cols ( carType, colour, owner). Query below is grouping on 2 cols ( carType, colour).My question is how can i make the grouping portion more dynamic by using reflection. my idea is add the grouping col in to array, then use the reflection on the query grouping part. however i was struck at the reflection..

XML
var gcols = new string[] { "CarType", "Colour" };
           var result = dt.AsEnumerable()
                                .GroupBy(x => new
                                {
                                    carType = x.Field<string>("carType"),
                                    colour = x.Field<string>("colour")
                                })
                                .Select(x => new
                                {
                                    CarType = x.Key.carType,
                                    Colour = x.Key.colour,
                                    count = x.Count()
                                })
                               .OrderBy(x => x.CarType).ToList();
Posted

XML
var gcols = new string[] { "CarType", "Colour" };
         var result = dt.AsEnumerable()
                              .GroupBy(x => new[]
                              {
                                  x.Field<string>(gcols.FirstOrDefault()),
                                  x.Field<string>(gcols.LastOrDefault())
                              })
                              .Select(x => new
                              {
                                  CarType = x.Key.Where(p=>p.Contains(gcols.FirstOrDefault())),
                                  Colour = x.Key.Where(p=>p.Contains(gcols.LastOrDefault())),
                                  count = x.Count()
                              })
                             .OrderBy(x => x.CarType).ToList();
   
Comments
Tina Ng 27-Apr-13 11:37am
   
how can i make the x.Field<string>(gcols.FirstOrDefault() more dynamic ? wha t if i have 10 items on gcols array then i need to add 10 row of (x.Field<string>(gcols.) ??
Faisal(mfrony) 29-Apr-13 23:18pm
   
I think his can help you
x.Field<string>(gcols[0])
The .QueryByCube() function provided by AdaptiveLINQ (www.adaptivelinq.com) is designed to make this kind of query (multi-dimesensional analysis query).
It doesn't use dynamic LINQ but transforms the query based on grouping criteria expressed by the selection.

For example, you can write:

"Count by type":
C#
dt.QueryByCube(yourCubeDefinition)
  .Select(x => new {
    Type = x.Type,
    Count = c.Count
  })

"Count by color":
C#
dt.QueryByCube(yourCubeDefinition)
  .Select(x => new {
    Color = x.Color,
    Count = c.Count
  })

"Count by type and color":
C#
dt.QueryByCube(yourCubeDefinition)
  .Select(x => new {
    Type = x.Type,
    Color = x.Color,
    Count = c.Count
  })
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900