User2294 User2294 - 2 years ago 93
C# Question

Cannot convert lambda expression to type 'bool' because it is not a delegate type?

I am working on an MVC, which displays a list of requests to a user. The model looks something like this:

public class RequestModel
{
public string Status { get; set; }
}


And the controller looks something like this:

public ActionResult Index()
{
return View(db.Requests.ToList());
}


I would like the index view page to show a list of the requests with status "Pending", which I have attempted with the following code:

<table class="table">
@foreach (var item in Model)
{
<tr>
<td>
@if (modelItem => item.Status == "Pending")
{
@Html.DisplayFor(modelItem => item.Status)
}
</td>
</tr>
}
</table>


However, this gives me the following error:


Cannot convert lambda expression to type "bool" because it is not a delegate type.


I am unsure what this errors means and how to work around it. If anyone has any help or advice I would greatly appreciate it.

Answer Source

Your condition should look like:

@if (item.Status == "Pending")

You're try to use lambda expression (modelItem => item.Status == "Pending") where you need simple condition.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download