Hello Team,
Please am having this error, kindly assist.
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 Room()
{
RoomViewModel objRoomViewModel = new RoomViewModel();
List
.Select(obj => new SelectListItem()
{
Text = obj.BookingStatus,
Value = Convert.ToInt32(obj.BookingStatusId).ToString(),
Selected = false
}).ToList();
List
.Select(obj => new SelectListItem()
{
Text = obj.RoomTypeName,
Value = Convert.ToInt32(obj.RoomTypeId).ToString(),
Selected = false
}).ToList();
objRoomViewModel.ListOfBookingStatus = bookingStatusList;
objRoomViewModel.ListOfRoomRtype = roomTypeList;
return View(objRoomViewModel);
}

Prasad RaveendranRank #320 · 7.8K reputationPosted Sep 9, 2023, 9:52 PM EDT
The error you're encountering, "LINQ to Entities does not recognize the method 'System.String ToString()'", occurs because LINQ to Entities doesn't support the
ToStringmethod within a query. In your code, you're trying to convert theobj.BookingStatusIdandobj.RoomTypeIdto strings within the LINQ query, and this is not allowed.To fix this issue, you should first retrieve the data from the database using LINQ to Entities and then perform the conversion to string outside of the LINQ query. Here's how you can modify your code:
public ActionResult Room() { RoomViewModel objRoomViewModel = new RoomViewModel(); List bookingStatusList = objHotelDbEntities.BookingStatus .AsEnumerable() // Switch to LINQ to Objects .Select(obj => new SelectListItem() { Text = obj.BookingStatus, Value = obj.BookingStatusId.ToString(), // Convert to string outside LINQ to Entities Selected = false }).ToList(); List roomTypeList = objHotelDbEntities.RoomTypes .AsEnumerable() // Switch to LINQ to Objects .Select(obj => new SelectListItem() { Text = obj.RoomTypeName, Value = obj.RoomTypeId.ToString(), // Convert to string outside LINQ to Entities Selected = false }).ToList(); objRoomViewModel.ListOfBookingStatus = bookingStatusList; objRoomViewModel.ListOfRoomRtype = roomTypeList; return View(objRoomViewModel); }By calling
AsEnumerable()before theSelectstatement, you switch from LINQ to Entities to LINQ to Objects, allowing you to use theToString()method for conversion without causing the error.Mohammad HussainRank #513 · 4.8K reputationPosted Sep 9, 2023, 9:15 PM EDT
public ActionResult Room() { RoomViewModel objRoomViewModel = new RoomViewModel(); // Fetch the data from the database first List bookingStatusList = objHotelDbEntities.BookingStatus.ToList(); List roomTypeList = objHotelDbEntities.RoomTypes.ToList(); // Then, create SelectListItem objects in memory List bookingStatusSelectList = bookingStatusList .Select(obj => new SelectListItem() { Text = obj.BookingStatus, Value = obj.BookingStatusId.ToString(), Selected = false }).ToList(); List roomTypeSelectList = roomTypeList .Select(obj => new SelectListItem() { Text = obj.RoomTypeName, Value = obj.RoomTypeId.ToString(), Selected = false }).ToList(); objRoomViewModel.ListOfBookingStatus = bookingStatusSelectList; objRoomViewModel.ListOfRoomRtype = roomTypeSelectList; return View(objRoomViewModel); }Amit MohantyRank #112 · 20K reputationPosted Sep 11, 2023, 2:02 AM EDT
public ActionResult Room() { RoomViewModel objRoomViewModel = new RoomViewModel(); List bookingStatusList = objHotelDbEntities.BookingStatus .ToList() .Select(obj => new SelectListItem() { Text = obj.BookingStatus, Value = obj.BookingStatusId.ToString(), Selected = false }).ToList(); List roomTypeList = objHotelDbEntities.RoomTypes .ToList() .Select(obj => new SelectListItem() { Text = obj.RoomTypeName, Value = obj.RoomTypeId.ToString(), Selected = false }).ToList(); objRoomViewModel.ListOfBookingStatus = bookingStatusList; objRoomViewModel.ListOfRoomRtype = roomTypeList; return View(objRoomViewModel); }Saravanan GanesanRank #153 · 15.1K reputationPosted Sep 24, 2023, 1:25 PM EDT
The error you're encountering, "LINQ to Entities does not recognize the method 'System.String ToString()'," occurs because you're trying to execute a
ToString()method inside a LINQ to Entities query. LINQ to Entities translates queries into SQL, and some operations, likeToString(), can't be translated directly.To fix this, you should materialize the query by calling
.ToList()before usingToString(). For example:Value = obj.BookingStatusId.ToString()
This way, the query results will be retrieved from the database, and then you can apply
ToString()without causing an error. Repeat this pattern for any other methods or conversions that can't be translated into SQL.