Hello Team,
Please am getting the below error from my controller, kindly assist me
Additional information: LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression.
public ActionResult Index()
{
RoomViewModel objRoomViewModel = new RoomViewModel();
objRoomViewModel.ListOfBookingStatus = (from obj in objHotelDbEntities.BookingStatus
select new SelectListItem()
{
Text = obj.BookingStatus,
Value = obj.BookingStatusId.ToString(),
Selected = false
}).ToList();
objRoomViewModel.ListOfRoomRtype = (from obj in objHotelDbEntities.RoomTypes
select new SelectListItem()
{
Text = obj.RoomTypeName,
Value = obj.RoomTypeId.ToString(),
Selected = false
}).ToList();
return View(objRoomViewModel);
}
Emmmanuel FIADUFEPosted Jul 21, 2023, 4:13 PM
Hello Naimish, please I did the correction as you indicated but the error still exist.
Additional information: LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression.
Naimish MakwanaPosted Jul 20, 2023, 12:21 PM
The error you are encountering is related to Entity Framework and LINQ to Entities. The issue arises because the
ToString()method is being used within the LINQ query, and Entity Framework is unable to translate it into a valid SQL query.To resolve this error, you should move the
ToList()method to execute the query against the database before calling theToString()method. Here's how you can modify your code to fix the issue:Thanks