user2470369
user2470369

Reputation: 167

Count rows in a table

How do I count rows in a table that meet a condition. Below counts every row regardless of whether or not a condition is met.

model IEnumerable<Sales1.Models.UserProfile>

@{
ViewBag.Title = "Details";
}

Total Sales Figures

@using (Html.BeginForm()) {
@Html.ValidationSummary(true)

foreach(var item in Model){

    if (item.Device_DeviceID != null)
    {
        @Model.Count()


    }

} }`

Upvotes: 0

Views: 4171

Answers (1)

Romias
Romias

Reputation: 14133

@Model.Count(x => x.Condition == Value)

If your condition is the Device_DeviceID not null, it became:

@Model.Count(x => x.Device_DeviceID != null)

Note that in your case, you don't need the foreach loop to do this.

Upvotes: 2

Related Questions