Below is my sample code which gets students details based on DepartmentId.
Public List<CommonDTO> GetStudentsList(int DepId)
{
List<CommonDTO> studentList = new List<CommonDTO>();
studentList = SBDB.Students.AsNoTracking()
.Where(a => a.DepartmentId == DepId)
.Select(s => new CommonDTO()
{
Id = Convert.ToInt32(s.StudentId),
DisplayName = SBDB.Users.Where(m => m.StudentId == s.StudentId).Select(x => x.DisplayName).FirstOrDefault()
})
.ToList();
return studentList;
}
In my database table the StudentId
is of type string, where as in my DTO class Id of type Int, the conversion inside the lambda expression is not allowed.
LINQ to Entities does not recognize the method 'Int32 ToInt32(System.String)' method, and this method cannot be translated into a store expression.
Any help on how to fix this ?