danosan 1
danosan 1

Reputation: 11

c# asp.net mvc - simplification

how can I simplify this composite condition?

    @if (Model.State.Inventory[0] == true
     && Model.State.Inventory[1] == true
     && Model.State.Inventory[2] == true
     && Model.State.Inventory[3] == true
     && Model.State.Inventory[4] == true
     && Model.State.Inventory[5] == true)
    {
        <a type="button" class="btn btn-secondary" asp-page="Cellar">Go to the Cellar</a>
    }

Upvotes: 1

Views: 59

Answers (1)

maccettura
maccettura

Reputation: 10829

This could be done using LINQ's Take() and All():

@if (Model.State.Inventory.Take(6).All(x => x == true))
{
    <a type="button" class="btn btn-secondary" asp-page="Cellar">Go to the Cellar</a>
}

Or if you are using C#8 you can use Ranges with LINQ:

@if (Model.State.Inventory.Inventory[..6].All(x => x == true))
{
    <a type="button" class="btn btn-secondary" asp-page="Cellar">Go to the Cellar</a>
}

Upvotes: 1

Related Questions