c# - c # using linq to group by multiple columns in a datatable -


i have 3 columns in datatable: string, datetime, , decimal. want group string , decimal column, , rows grouped want sum decimal values. know how sum part, how group 2 different columns in datatable?

this code far doesn't work properly:

var newsort = row in objecttable.asenumerable()               group row new {id = row.field<string>("resource_name"), time1 = row.field<datetime>("day_date")} grp               orderby grp.key               select new               {                  resource_name1 = grp.key.id,                  day_date1 = grp.key.time1,                  sum = grp.sum(r => r.field<decimal>("actual_hrs"))               }; 

i don't think you're giving full story. other orderby not working anonymous types (the code gave wouldn't have compiled), query should work way want. put in linqpad:

var objecttable = new datatable(); objecttable.columns.add("resource_name",typeof(string)); objecttable.columns.add("day_date",typeof(datetime)); objecttable.columns.add("actual_hrs",typeof(decimal)); objecttable.rows.add(1, datetime.today, 1); objecttable.rows.add(2, datetime.today, 2);  var newsort = row in objecttable.asenumerable()             group row new {id = row.field<string>("resource_name"), time1 = row.field<datetime>("day_date")} grp             select new                 {                     resource_name1 = grp.key.id,                     day_date1 = grp.key.time1,                     sum = grp.sum(r => r.field<decimal>("actual_hrs"))                 }; newsort.dump(); 

... , got these results:

resource_name1 | day_date1            | sum 1              | 7/1/2011 12:00:00 | 1  2              | 7/1/2011 12:00:00 | 2  

Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -