列表应该由作为列表中对象的子属性的属性排序。按属性子属性排序列表元素
pList = pList
.OrderBy(x => x.GetType()
.GetProperty(sortBasedValue)
.GetValue(x, null))
.ToList();
将排序列表中的所有元素,但某些子元素具有多个属性。 已经尝试subitem.value
作为sortBasedValue
但将无法正常工作。
列表应该由作为列表中对象的子属性的属性排序。按属性子属性排序列表元素
pList = pList
.OrderBy(x => x.GetType()
.GetProperty(sortBasedValue)
.GetValue(x, null))
.ToList();
将排序列表中的所有元素,但某些子元素具有多个属性。 已经尝试subitem.value
作为sortBasedValue
但将无法正常工作。
的getProperty需要的属性作为参数的名称,但可以将其直接不使用陈伟伟其财产的财产。你需要链接它。
Enumerable.Empty<object>()
// looks from the naming its a private variable,
//so you might want to call it via , GetProperty("subitem", BindingFlags.NonPublic)
.OrderBy(x =>
{
var subitem = x.GetType().GetProperty("subitem").GetValue(x);
return subitem.GetType().GetProperty("value").GetValue(subitem);
})
.ToList();
如果你有pList
的类型,我会建议你使用一个属性选择器又名Func<TObject, TPropertyToOrderBy>
,并把它交给OrderBy
。
您需要实现自定义IComparer并使用排序方法。
的解决方案是在这里C# Sort and OrderBy comparison
所以'sortBasedValue'是一个像“'Name”这样的字符串,并且您想要传递一个像“'Address.City”'这样的chid属性? –
@DStanley最有可能的是,因为GetProperty只将它作为第一个参数。 – Xiaoy312
@ Xiaoy312我知道'GetProerty'是如何工作的,我更关心OP如何通过这个子属性。 –