Marius Vasile

Marius Vasile

  • 1.1k
  • 1.1k
  • 35.3k core sum of column integer

Dec 1 2021 9:35 AM

I am using Razor Pages and I have a table and the field I want to sum is declared as int

public int PunctajGrilaIntrebare { get; set; }

I want to calculate the Sum for all values in the table so I use

var tp = GrilaClassSecondariesList.Sum(s => s.PunctajGrilaIntrebare);

However, the result in the View is 0

When I list the values in the View with

@foreach(var item in Model.GrilaClassSecondariesList)
  @Html.DisplayFor(m => item.PunctajGrilaIntrebare)

I have 0 2 and 4, as stored in the database.

If I use in the View

@Model.GrilaClassSecondariesList.Sum(s => s.PunctajGrilaIntrebare)

I get the sum, 6 but I want to use this in code behind. How do I do this?

Answers (2)