mukesh salaria

mukesh salaria

  • NA
  • 20
  • 35.9k

Group by with lambda expression in c# with mvc

Sep 10 2014 2:27 AM
Hi Friends,
 
I have a lambda expression to group by items with two columns Cost_Code and Category, Here my query
 
IEnumerable<CJTViewModel> objModel = (from q in db.CURRENT_JCT_TRANSACTION
where q.Job == job
group q by new { q.Cost_Code, q.Category } into g
select new CJTViewModel()
{
Job = job,
CJT_EQ_Cost = g.Key.Category == "E" ? g.Sum(s => s.Amount) : 0,
CJT_OH_Cost = g.Key.Category == "OH" ? g.Sum(s => s.Amount) : 0,
Cost" && q.Category == "L") ? q.Amount : 0,
CJT_PR_Cost = g.Key.Category == "LB" ? g.Sum(s => s.Amount) : 0,
Cost_Code = g.Key.Cost_Code,
Category=g.Key.Category
}).ToList();
ViewBag.ExpensesPosted = objModel; 
 
My View code
 
@foreach (var detail in ViewBag.ExpensesPosted)
{
<tr>
<td>@detail.Job</td>
<td>@detail.Cost_Code</td>
<td>@detail.CJT_EQ_Cost</td>
<td>@detail.CJT_OH_Cost</td>
<td>@detail.CJT_PR_Cost</td>
<td>@detail.CJT_AP_Material_Cost</td>
</tr>
}
 
it gives me result like
 
job Cost_Code EQ_Cost OH_Cost PR_Cost
--------------------------------------------------------
102   22                $30           $10           $5
102   23                 $0             $15          $20
102   24                 $10           $0             $20
 
but i wants output like below
 
job Cost_Code Category
--------------------------------------------------------
102     22            EQ_Cost    $30
                            OH_Cost    $10
                            PR_Cost     $5 
                           ---------------------
                                                $45
102     23            OH_Cost    $15
                             PR_Cost    $20 
                             --------------------
                                                 $35 
102 24                EQ_Cost     $10
                           PR_Cost       $20
                            --------------------
                                                 $30 
 
please help