2

I'm trying to populate a combobox in a creation view inside MVC 3. This is what i've done so far:

    public ActionResult Create()
    {
        var db = new ErrorReportingSystemContext();
        IEnumerable<SelectListItem> items = db.Locations
          .Select(c => new SelectListItem
          {
              Value =c.id,
              Text = c.location_name
          });
        ViewBag.locations = items;
        return View();
    } 

However when i try to run it it gives a compilation error:

Cannot implicitly convert int to string

In this post i read that doing

Value = SqlFunctions.StringConvert((double)c.ContactId)

would fix the problem however when i try to do that i get the following error:

the name 'SqlFunctions' does not exist in the current context

What i'm i doing wrong?

Update:

doing Value = c.id.ToString() gives the error:

LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression.

Community
  • 1
  • 1
KoU_warch
  • 2,160
  • 1
  • 25
  • 46

3 Answers3

7

Your problem is that EF cannot translate the casting to string or the .ToString() method.

So you need to evaluate the DB query (with calling .AsEnumerable()) before selecting into the SelectListItems

IEnumerable<SelectListItem> items = db.Locations
      .AsEnumerable()
      .Select(c => new SelectListItem
      {
          Value = c.id.ToString(),
          Text = c.location_name
      });

However this approach has some performance problems because the generated SQL query will look like this:

SELECT * FROM Locations ...

So if the Locations table has 50 columns EF will load the data from all them although later you only need data from two columns.

You can tell to EF which columns should it load with first selecting into an anonymous type then into the SelectListItems:

IEnumerable<SelectListItem> items = db.Locations
      .Select(c => new
      {
          c.id,
          c.location_name
      });
      .AsEnumerable()
      .Select(c => new SelectListItem
      {
          Value = c.id.ToString(),
          Text = c.location_name
      });

And the generated query will look like somehting like this:

 SELECT id, location_name FROM Locations
nemesv
  • 138,284
  • 16
  • 416
  • 359
  • Thank you very much, i just tried the first one and it worked like a charm!. If you don't mind, can you explain better the difference between the two? i didn't understand that about the columns that you posted – KoU_warch Sep 17 '12 at 20:36
  • @EH_warch I've extended my answer, I hope it's more clear now. – nemesv Sep 18 '12 at 04:49
0

It's probably complaining that Value expects a string and you're trying to store an int in it.

Try:

Value = c.id.ToString(),
Jamie Dixon
  • 53,019
  • 19
  • 125
  • 162
  • I get the following error `LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression.` – KoU_warch Sep 17 '12 at 17:00
0
foreach (var item in db.table.tolist())
{
                Combobox.Items.Add(item.field.tostring());
}
Joshua Van Hoesen
  • 1,684
  • 15
  • 30