DropDown Filter using C# MVC 4

Question:

I have a dropdown in my view that I populate as follows:

 ViewBag.NfeStatus = EnumHelper
      .ListAll<NfeStatus>()
      .ToSelectList(x => x, x => x.Description());

My NfeStatus model is an enum:

public enum NfeStatus
{
    [Description("-")]
    NotIssued = 0,
    [Description("Pendente")]
    Pending,
    [Description("Erro")]
    Error,
    [Description("Ok")]
    Ok
}

But I would like not to display the "OK" option.

How do I make this filter?

Answer:

Add a filter using .Where() .

Something like .Where(x => x != NfeStatus.Ok) .

Or .Where(x => x.Description() != "OK") if you prefer to search by description, although I don't think there's any reason to.

Example

ViewBag.NfeStatus = EnumHelper.ListAll<NfeStatus>()
                              .Where(x => x != NfeStatus.Ok)
                              .ToSelectList(x => x, x => x.Description());

Or, using the description:

ViewBag.NfeStatus = EnumHelper.ListAll<NfeStatus>()
                              .Where(x => x.Description() != "OK")
                              .ToSelectList(x => x, x => x.Description());
Scroll to Top