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 RaveendranPosted Sep 10, 2023, 1:52 AM
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:
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.Saravanan GanesanPosted Sep 24, 2023, 5:25 PM
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.Amit MohantyPosted Sep 11, 2023, 6:02 AM
Mohammad HussainPosted Sep 10, 2023, 1:15 AM