如何orderby使用我作为参数的值指定传递给参数的参数?例如:List<Student> existingStudends = new List<Student>{ new Student {...}, new Student {...}}目前执行:List<Student> orderbyAddress = existingStudends.OrderBy(c => c.Address).ToList();而不是c.Address,我如何将其作为参数?例 string param = "City"; List<Student> orderbyAddress = existingStudends.OrderByDescending(c => param).ToList();
3 回答
猛跑小猪
TA贡献1858条经验 获得超8个赞
这是使用反射的可能性...
var param = "Address";
var propertyInfo = typeof(Student).GetProperty(param);
var orderByAddress = items.OrderBy(x => propertyInfo.GetValue(x, null));
- 3 回答
- 0 关注
- 803 浏览
添加回答
举报
0/150
提交
取消