cramar
cramar

Reputation: 134

Returning a list of items from a database table

I am not sure what I am doing wrong. I want to return a list of value/item pairs to be used in a dropdown box. I get the following error.

Cannot implicitly convert type 
System.Collections.Generic.List<System.Web.Mvc.SelectListItem> to
System.Collections.Generic.List<string>

My method causing the error is below.

Method

    //Get the shift list based on StationId which is foreign key in Shifts table.
    public List<string> GetShiftsByStationId(int stationId)
    {
        //Created DataContext to query DB.
        using (DataManager db = new DataManager())
        {
            //returns all the records from table based on StationId in list format.
            return db.Shifts.Where(query => query.StationId == stationId).Select(q => new SelectListItem { Value = q.ShiftId.ToString(), Text = q.Code }).ToList();
        }
    }

Upvotes: 0

Views: 667

Answers (1)

ramiramilu
ramiramilu

Reputation: 17182

public List<string> GetShiftsByStationId(int stationId)

Should be as shown below (because in your LINQ query you are selecting new SelectListItem)

public List<SelectListItem> GetShiftsByStationId(int stationId)

Upvotes: 5

Related Questions