tags:

views:

72

answers:

0

I have a datatable like below, the Sales team can have any numbers (in the below example only two columns sales1and sales2)

FactoryName MachineName StartTime                 Sales1 Sales2

Fact 1      M1          2009-11-06T00:00:00+05:30 1      2

Fact 1      M2          2009-11-06T00:00:00+05:30 1      2

Fact 2      M1          2009-11-06T00:00:00+05:30 5      6

Fact 2      M2          2009-11-06T00:00:00+05:30 7      7

Fact 2      M3          2009-11-06T00:00:00+05:30 8      8

Fact 1      M1          2009-11-07T00:00:00+05:30 9      9

Fact 1      M2          2009-11-07T00:00:00+05:30 10     100

Fact 2      M1          2009-11-07T00:00:00+05:30 15     1

Fact 2      M2          2009-11-07T00:00:00+05:30 12     2

Fact 2      M3          2009-11-07T00:00:00+05:30 16     3

I want to write a linq query which should return me like below ,for each factoryname,day I need the sum of sales1 and sales2 ( this sales 1 and sale2 is not static columns this can be any noumber of columns) ,basically I need help for achieving this using dynamic linq

FactoryName StartTime                 Sales1  Sales2

Fact1       2009-11-06T00:00:00+05:30 2       4

Fact2       2009-11-06T00:00:00+05:30 20      21

Fact1       2009-11-06T00:00:00+05:30 19      109

Fact2       2009-11-06T00:00:00+05:30 43      6

 

var MyQuery = (from table in myDataset.Tables[0].AsEnumerable()
                                  orderby table["StartTime"]
                                  group table by new { FactoryName = table["FactoryName"], StartTime = table["StartTime"] }
                                      into myGroupedTable
                                      select new
                                      {
                                          FactoryName = myGroupedTable.Key.FactoryName,
                                          StartTime = myGroupedTable.Key.StartTime,
                                          sales1 = myGroupedTable.Sum(table => table.Field<Double?>("sales1")),
       sales2 = myGroupedTable.Sum(table => table.Field<Double?>("sales2"))
                                      });

the above query returns me the data which I want ,but my problem is in the dataset (myDataset.Tables[0]) I may have any number of columns for sales ( like sales1,sales2,sales3 ,sales4 etc...) I need the sum of all those sales columns independently .How can I do that ? please help me with some code snippet