1

I would like to sum values I get from two columns, these values are added from the data collected in View page based on customers so adding it in a controller from database is not an option as far as I know, I could be wrong Any help would be appreciated.

I can get sum of two columns individually but can't get sum of two columns.

<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.PkgBasePrice)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Description)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.BasePrice)
        </th>
    </tr>

    @foreach (var item in Model.Where(ModelItem => 
ModelItem.CustomerId.Equals(Session["CustomerId"])))
    {
    <tr>

        <td>
            @Html.DisplayFor(modelItem => item.PkgBasePrice)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Description)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.BasePrice)
        </td>

    </tr>
    }

</table>
<h2>
    Package Total: [email protected](c => 
c.CustomerId.Equals(Session["CustomerId"])).Sum(b => b.PkgBasePrice)

</h2>
<h2>
    Product Total: [email protected](c => 
c.CustomerId.Equals(Session["CustomerId"])).Sum(b => b.BasePrice)

</h2>

I would like to get sum of PkgBasePrice and BasePrice in one amount

3
  • I think you're overcomplicating this. if you can get the sum of 2 separate things and your goal is to get the sum of those 2 separate sums, then just add them together. Model.Single(ModelItem => ModelItem.CustomerId.Equals(Session["CustomerId"])).Sum(x => x.PkgBasePrice) + Model.Single(ModelItem => ModelItem.CustomerId.Equals(Session["CustomerId"])).Sum(x => x.BasePrice) ? Commented Feb 4, 2019 at 18:59
  • you need to wrap it like so @(Model..... + Model....) see my answer Commented Feb 4, 2019 at 19:18
  • itd help if you posted your model class. check the updated answer Commented Feb 4, 2019 at 19:26

1 Answer 1

1

Since you already have the two individual sums you want to combine, you can add the two sums you have together as follows:

@(@Model.Where(c => c.CustomerId.Equals(Session["CustomerId"])).Sum(b => b.PkgBasePrice) + 
    @Model.Where(c => c.CustomerId.Equals(Session["CustomerId"])).Sum(b => b.BasePrice))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.