Kirsty White
Kirsty White

Reputation: 1220

if model has a certain value then count

My Model.PendingActivation (int?) can be 0, 1 or Null. 1 is pending, I want to count how many 1s there are and display that count in my view?

@if (Model.PendingActivation.HasValue == "1")
{
  <a>@Html.Encode(Model.PendingActivation.Count)</a>
}  

Not sure how I can count how many are pending.

Upvotes: 0

Views: 116

Answers (2)

Patrick Hofman
Patrick Hofman

Reputation: 156938

Assuming Model is a list of some type that implements IEnumerable<T>, you can Count() them:

<a>@Html.Encode(Model.Count(m => m.PendingActivation != null && m.PendingActivation.HasValue && m.PendingActivation.Value == 1))</a>

The code between Count() is the expression that checks every item in the list, in this case you want to have PendingActivation == 1, which this expression checks.

Upvotes: 3

JeffersonAmori
JeffersonAmori

Reputation: 1

You can do:

<a>@Html.Encode(Model.FindAll(m => m.PendingActivation == 1).Count()</a>

Upvotes: 0

Related Questions