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 SwainRank #165 · 14.4K reputationPosted May 16, 2023, 2:11 PM EDT
public IEnumerable GetAllCustomer() { IEnumerable objSelectListItems = new List(); var customers = objRestaurantDBEntities.tblCustomers.ToList(); objSelectListItems = customers.Select(obj => new SelectListItem { Text = obj.CustomerName, Value = obj.CustomerId.ToString(), Selected = true }).ToList(); return objSelectListItems; }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.
Emmmanuel FIADUFERank #947 · 2.2K reputationPosted May 16, 2023, 4:12 PM EDT
Thank you Swain for your help, I really appreciate it,
Everything is working now.
Rajkiran SwainRank #165 · 14.4K reputationPosted May 17, 2023, 12:54 AM EDT
Please accept the answer , if it is helpful.