korn
korn

Reputation: 67

LINQ statement - WHERE clause for ENUM type

I followed MSDN tutorial about filtering functionality in ASP.NET MVC web app > http://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/sorting-filtering-and-paging-with-the-entity-framework-in-an-asp-net-mvc-application

Unfortunately, I can't select an object which has an enum attriubute.

Used model is:

public Bus()
    {
    public int BusID { get; set; }
    public string RegNum { get; set; }
    public Status? Status { get; set; }
    public string Latitude { get; set; }
    public string Longitude { get; set; }
    }

where the enum type is:

public enum Status
{
    ON, OFF
}

According to the mentioned tutorial I implemented sorting method in BusController as shown below:

public ViewResult Index(string searchString)
    {
        var buses = from b in db.Buses select b;
        if (!String.IsNullOrEmpty(searchString))
        {
            buses = buses.Where(b =>
                b.RegNum.ToUpper().Contains(searchString.ToUpper())
                );
        }

        return View(buses.ToList());
    }

it works fine for RegNum filtering, but I can't select the Bus for the given status by searchstring value.

b.Status.Equals(searchstring) doesn't work.

I'd be grateful for any hints

Upvotes: 1

Views: 7136

Answers (2)

Konstantin
Konstantin

Reputation: 328

Try the following changes:

 public class Bus
{
    public int BusID { get; set; }
    public string RegNum { get; set; }
    public Status Status { get; set; }
    public string Latitude { get; set; }
    public string Longitude { get; set; }
}

Take Status as second parameter and filter on both. Sample code below.

       public static IEnumerable<Bus> FilterBuses(string searchString, Status status)
    {
        //Setup some dummy data
        var buses = new List<Bus>()
        {
            new Bus() { BusID = 12, RegNum = "Twelve", Status = Status.ON},
            new Bus() { BusID = 13, RegNum = "Thirteen", Status = Status.OFF},
            new Bus() { BusID = 20, RegNum = "Twenty", Status = Status.OFF}
        };

        IEnumerable<Bus> filteredList = new List<Bus>();
        if (!String.IsNullOrEmpty(searchString))
        {
            filteredList = buses.Where<Bus>(b => 
                b.RegNum.ToUpper().Contains(searchString.ToUpper()) &&
                b.Status == status);
        }

        return filteredList.ToList();
    }

Then, you can call the method as follows:

        // Expect only Twelve bus
        var result = LinqTestMethod.FilterBuses("Twelve", Status.ON);

        // Expect no buses
        result = LinqTestMethod.FilterBuses("Twelve", Status.OFF);

        // Expect 2 buses -- Twelve and Twenty
        result = LinqTestMethod.FilterBuses("T", Status.OFF);

Upvotes: 0

juharr
juharr

Reputation: 32266

That's because Status is not a string. You probably want to convert the searchString to a Status with

Status searchStatus = Enum.Parse(typeof(Status), searchString.ToUpper());

first then use that in your query (NOTE: Do not put the parsing code directly into the query). Or Enum.TryParse to avoid potential exceptions. Also note that what is saved in your DB with be an int, 0 for ON and 1 for OFF.

Upvotes: 2

Related Questions