Hello Team,
Am created repository for my Entity data and this the error I get when I run the code.
Additional information: LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression.
CLASS
public partial class tblCustomer
{
public int CustomerId { get; set; }
public string CustomerName { get; set; }
}
REPOSITORIES CLASS
namespace SmartRestaurantManagement.Repositories
{
public class CustomerRepository
{
private readonly RestaurantDBEntities objRestaurantDBEntities;
public CustomerRepository()
{
objRestaurantDBEntities = new RestaurantDBEntities();
}
public IEnumerable
{
IEnumerable
objSelectListItems = (
from obj in objRestaurantDBEntities.tblCustomers
select new SelectListItem
{
Text = obj.CustomerName,
Value = obj.CustomerId.ToString(),
Selected = true
}).ToList();
return objSelectListItems;
}
}
}
Rajkiran SwainPosted May 16, 2023, 6:11 PM
The error occurs when trying to convert the
CustomerIdto a string using theToString()method. This is because LINQ to Entities does not support theToString()method in a query that is being translated to a store expression.To resolve this issue, you can modify the code by first fetching the data from the database and then performing the conversion to a string outside of the LINQ query.
Rajkiran SwainPosted May 17, 2023, 4:54 AM
Please accept the answer , if it is helpful.
Emmmanuel FIADUFEPosted May 16, 2023, 8:12 PM
Thank you Swain for your help, I really appreciate it,
Everything is working now.