【发布时间】:2019-12-26 23:56:05
【问题描述】:
我正在从 .NET Core 2 或 3 版本迁移现有的 Web API。 经过几个问题,我设法使它工作,除了按列名的动态排序。
这是我的代码,在 .net core 2 上运行良好:
public async Task<IEnumerable<Clientes_view>> GetClientes(int bActivos, int nRegistroInic, int nRegistros, string sOrdenar,
int nSentido, string sFiltro, int nTipo = -1, int idCliente = -1)
{
var clientes = this.context.Set<Clientes_view>()
.Where(e => e.RazonFantasia.Contains(sFiltro) || e.RazonFantasia.Contains(sFiltro)
|| e.Cuit.Contains(sFiltro) || e.Mail.StartsWith(sFiltro) || string.IsNullOrEmpty(sFiltro))
.Where(e => (e.Activo && bActivos == 1) || bActivos == -1 || (!e.Activo && bActivos == 0))
.Where(e => e.IdTipoCliente == nTipo || nTipo == -1)
.Where(e => e.IdCliente == idCliente || idCliente == -1);
if (!string.IsNullOrEmpty(sOrdenar))
{
var propertyInfo = this.context.Set<Clientes_view>().First().GetType().GetProperty(sOrdenar,
BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance);
if (propertyInfo != null) if (nSentido == -1) clientes = clientes.OrderByDescending(e => propertyInfo.GetValue(e, null));
else clientes = clientes.OrderBy(e => propertyInfo.GetValue(e, null));
}
clientes = clientes.Skip(nRegistroInic).Take(nRegistros);
return await clientes.ToListAsync();
}
我得到的错误如下:
System.InvalidOperationException:LINQ 表达式 'DbSet .Where(c => True) .Where(c => c.Activo && True || False || False) .Where(c => True) .Where(c => True) .OrderBy(c => __propertyInfo_3.GetValue( 对象:c, index: null))' 无法翻译。以可翻译的形式重写查询,或通过插入对 AsEnumerable()、AsAsyncEnumerable()、ToList() 或 ToListAsync() 的调用显式切换到客户端评估。
有什么想法吗? 谢谢!
【问题讨论】:
-
它的 EFcore 对吗?
-
是的 EFCore 是
-
因为我的 Web API 控制器将列名作为查询字符串进行排序。并且前端应用程序不是 NET 客户端。基本上我需要按列名(字符串)对查询进行排序
-
entityframeworkcore.com/knowledge-base/54232892/… 我认为你需要使用 OrderBy("String_ColumnName")
-
哇,谢谢,这正是我需要的!该库与 .net Core 3 兼容吗?
标签: c# linq entity-framework-core ef-core-3.0