0

I got this error

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

What I am trying to do is to list all of my values in my database.

Here is the code in my view:

  <div class="col-md-10">
      @Html.DropDownList("SelectedScheduleId", Model.ScheduleSelectListItems, htmlAttributes: new { @class = "form-control" })
      @Html.ValidationMessageFor(model => model.sched_id, "", new { @class = "text-danger" })
  </div>

Here is my class in my model:

  public class BookingModel
    {
        [Required]
        [StringLength(200, ErrorMessage = "Description too short. Must be at least {2} characters long", MinimumLength = 10)]
        [Display(Name = "Describe your Consultation")]
        public string appointment_description { get; set; }
        public int appointment_id { get; set; }
        public int student_id { get; set; }
        public int sched_id { get; set; }
        public sched sched { get; set; }
        public IEnumerable<SelectListItem> ScheduleSelectListItems { get; set; }
        public int SelectedScheduleId { get; set; }
    }

and my controller:

public ActionResult Book()
{
    var items = db.scheds.Select(sched => new SelectListItem { Text = string.Format("{0} - {1}", sched.sched_stime, sched.sched_etime), Value = sched.sched_id.ToString() });

    var model = new BookingModel { ScheduleSelectListItems = items };
    return View(model);
}

I just want to place the values of my sched table into a dropdown list.

I have consulted someone about how to list values of the foreign key and now I've ended up with a new error. How can I fix this? This is the only thing standing between me and finishing my project. :(

teo van kot
  • 12,350
  • 10
  • 38
  • 70
m1gs
  • 197
  • 2
  • 17

1 Answers1

1

I suppose it's clear you can't use string.Format when you working with LinqToSQL.

You have 2 options:

var items = db.scheds
   .ToList()
   .Select(sched => new SelectListItem 
   { 
      Text = string.Format("{0} - {1}", sched.sched_stime, sched.sched_etime),    
      Value = sched.sched_id.ToString() 
   });

Use .ToList() to populate your table from db to server. Then you will work with LinQToObjects when you call string.Format.

Or concatenate your string without string.Format:

Text = sched.sched_stime + " - " + sched.sched_etime,

Then LinQToSQL be able to translate it.

teo van kot
  • 12,350
  • 10
  • 38
  • 70
  • `I suppose it's clear...` - apparently not, otherwise the question wouldn't be asked :-) – Maarten Dec 08 '15 at 12:10
  • @Maarten You right, that's why i explain it in details ;) – teo van kot Dec 08 '15 at 12:11
  • Also, don't confuse **LinqToSql** with **Entity Framework**, they are not the same. – Maarten Dec 08 '15 at 12:11
  • wow thank you so much! sorry I couldn't understand the solutions that was already been given. I am having a hard time how can I implement those to my code. I would study on how the result happened. Thank you again sir! – m1gs Dec 08 '15 at 13:54
  • I noticed that the sched_id was returned as a string. can i possibly return back as an int? @teovankot – m1gs Dec 08 '15 at 16:11
  • @MichaelZ no you have to option becouse you use `SelectListItem` and actually you don't need it - everything is string in html. – teo van kot Dec 08 '15 at 20:32