Group By Sum Linq to SQL in C# -
really stuck linq sql grouping , summing, have searched everywhere don't understand enough apply other solutions own.
i have view in database called view_projecttimesummary, has following fields:
string_userdescription string_projectdescription datetime_date double_hours i have method accepts , date parameter , first creates list<>:
list<view_usertimesummary> view_usertimesummarytoreturn =               (from linqtable_usertimesummaryview               in datacontext_usertimesummary.gettable<view_usertimesummary>()               linqtable_usertimesummaryview.datetime_week <= datetime_to               && linqtable_usertimesummaryview.datetime_week >= datetime_from               select linqtable_usertimesummaryview).tolist<view_usertimesummary>(); before returning list (to used datasource datagridview) filter string_userdescription field using parameter of same name:
if (string_userdescription != "")         {             view_usertimesummarytoreturn =                          (from c in view_usertimesummarytoreturn                          c.string_userdescription == string_userdescription                          select c).tolist<view_usertimesummary>();         }  return view_usertimesummarytoreturn; how manipulate resulting list<> show sum of field double_hours user , project between , date parameters (and not separate entries each date)?
e.g. list<> following fields:
string_userdescription string_projectdescription double_sumofhoursbetweentoandfromdate am right mean have return different type of list<> (since has less fields view_usertimesummary)?
i have read sum it's 'group / / b' don't understand how syntax works looking @ other solutions... can please me?
thanks steve
start out defining class hold result:
public class groupedrow {   public string userdescription {get;set;}   public string projectdescription {get;set;}   public double sumofhoursbetweentoandfromdate {get;set;} } since you've applied filtering, thing left group.
list<groupedrow> result = (   row in source   group row new { row.userdescription, row.projectdescription } g   select new groupedrow()   {     userdescription = g.key.userdescription,     projectdescription = g.key.projectdescription,     sumofhoursbetweentoandfromdate = g.sum(x => x.hours)   } ).tolist(); (or other syntax)
list<groupedrow> result = source   .groupby(row => new {row.userdescription, row.projectdescription })   .select(g => new groupedrow()   {     userdescription = g.key.userdescription,     projectdescription = g.key.projectdescription,     sumofhoursbetweentoandfromdate = g.sum(x => x.hours)   })   .tolist(); 
Comments
Post a Comment