I have migrated my application from dotNet core2.1 to Dotnet core5. The below function has stopped working. I tried to debug the code but I was not able find the solution. The line which is giving error is X => Convert.ToInt32(X.Value)
. If I don't do the conversion function works fine but the order of list gets disturbs.
Error :-
The LINQ expression 'DbSet<EbDepartmentMsts>()\r\n .OrderBy(e => Convert.ToInt32(e.DeptCode.ToString()))' could not be translated. Additional information: Translation of method 'object.ToString' failed. If this method can be mapped to your custom function, Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'
function
public List<SelectListItem> GetDepartmentCode()
{
var table = new List<SelectListItem>();
try
{
table = (from eb in _context.EbDepartmentMsts
select new SelectListItem
{
Text = string.Concat(eb.DeptCode.ToString(), "~", eb.Description),
Value = eb.DeptCode.ToString()
}).OrderBy(X => Convert.ToInt32(X.Value)).ToList();
}catch(Exception ex)
{
}
return table;
}