Mhmd Mhmd - 3 months ago 8
ASP.NET (C#) Question

Hiding button if all status is "Paid" using LINQ

I'm working on ASP.NET MVC project where I want to display and hide a button when all Status is "Paid".

The Status sometimes will be list of Status and sometimes will be single Status.
I need a code using LINQ to check if the all Status are "Paid",hide this button.
here is the code and what i'm trying to do...

@foreach (var item in Model.PaymentList)
{
//Here i want to check if all item.Status=="Paid"
if(item.Status=="Paid")
{
//if true i want to display my button
<button type="button">Click Me!</button>
}
else
{
<button type="button" >Click Me!</button>
else hide my button
}
}


i want to know if i can do this using LINQ,Thank you

Answer

You can use the LINQ All method.

@if(!Model.PaymentList.All(f=>f.Status=="Paid"))
{
    <button>click me</button>
}

Or the Any method

@if(Model.PaymentList.All(f=>f.Status!="Paid"))
{
    <button>click me</button>
}