【发布时间】:2021-12-28 03:23:12
【问题描述】:
我已将我的应用程序从 dotNet core2.1 迁移到 Dotnet core5。以下功能已停止工作。我尝试调试代码,但找不到解决方案。给出错误的行是X => Convert.ToInt32(X.Value)。如果我不这样做,转换功能可以正常工作,但列表的顺序会受到干扰。
错误:-
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'
功能
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;
}
【问题讨论】:
-
物化 LINQ 查询然后
Select。from eb in _context.EbDepartmentMsts.ToList(). -
@YongShun 但是当我使用 .net core sdk 2.1 时,相同的代码更早地工作了