Skip to content

How to show calculated field in the index view?

Arun Khatri

Asked Mar 21, 2016, 7:22 PM EDT3 answers
Share
LinkedInX
In an MVC project I have the following two tables:
 
CaseData (CaseId,CaseNo )
Invoice (InvoiceId, CaseId, Amount)
 
CaseId, the primary key in table CaseData is a foreign key in table Invoice. There is One to Many relationship between table CaseData and Invoice.
 
What I want to do is to show Sum Total of the field Amount against each record of the table CaseData in the Index view.
 
Please consider that I am using Entity Framework to access data in my project. 

2 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • Suthish Nair
    0
    Use Group By on Caseid or CaseNo. Do not wait for ready made answers, try yourself, which helps you to learn.
    0
  • 0
    Dear Suthish Nair,
     
    It is not that I do not know how to write the query to get the desired result, here is the query which works absolutely well:
     
    Code
    var invoice = db.tblInvoices
                                     .GroupBy(x => x.fldCaseId)
                                     .Select(g => new { fldCaseId = g.Key, Amount = g.Sum(s => s.fldAmount) });
     
    The problem is I am not able to pass it on to the view through the associated ViewModel so that I get the Amount against each appended row in the index view.
     
    Please help. 
    0
  • 1 short reply
    • 0
      Thanks!
      0