c# - 使用反射 linq c# 进行排序
问题描述
我正在尝试使用反射对我的记录进行排序
IQueryable<Container> containers = this.Repository.Containers;
foreach (var prop in typeof(Container).GetProperties())
{
if (sortField.IndexOf(prop.Name, StringComparison.OrdinalIgnoreCase) >= 0)
{
containers = sortDir == "desc"
? containers.OrderByDescending(x => prop.GetValue(x, null))
: containers.OrderBy(x => prop.GetValue(x, null));
}
}
var total = await containers.CountAsync();
... Skip/Take actions ...
但执行此行后出现错误var total = await containers.CountAsync();
The LINQ expression '(c) => (Unhandled parameter: __prop_2).GetValue(
obj: c.Outer,
index: null)' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to either AsEnumerable(), AsAsyncEnumerable(), ToList(), or ToListAsync(). See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.
我的代码有什么问题?
解决方案
推荐阅读
- reactjs - 给反应无状态功能组件一个打字稿类型的正确方法是什么
- php - 如何从数据库中获取特定行?
- rust - 检查 HashSet 中是否包含字符串切片
> 不分配新字符串 - angular - 如何正确制作放置在与项目/dockerfile不同的路径中的Docker Compose?
- python - 以实数形式将时间跨度字符串转换为秒
- python - 如何进行仅类型注释的类型断言?
- powershell - 在 Hyper-V 主机和来宾之间的 -FromSession 脚本中使用 Copy-Item 失败
- python - 在 python 3 中以格式给出'SyntaxError: invalid syntax'
- c - 便携式一致浮动
- node.js - Nodejs不会只为一条路线搜索公共文件夹中的文件