排序列表< T>在< U>类型的成员上在运行时决定。 [英] Sorting List<T> on a member of type<U> decided at runtime.
问题描述
我有一个类型的客户为
I have a type Customer as
Class Customer
{
string Name;
Address address;
...
}
和班级地址为
And class Address as
Class Address
{
string Street;
int HouseNum;
....
}
我创建了一个客户列表
I am creating a list of Customer as
List<Customer> CustSource= new List<Customer>();
然后我将CustSource绑定到转发器。
Repeater有一个名为'Street'的列绑定了`CustSource.address.Street`,'House Number'绑定了`CustSource.address.HouseNum`,'Name'绑定了`CustSource.Name`等等。
我想在运行时对绑定源进行排序,即CustSource。我知道当我在编译时知道成员时,它可以作为CustSource.OrderBy(x => x.Name)完成。为了确定成员在运行时进行排序,我在本文的帮助下编写了一个扩展方法: http://stackoverflow.com/questions/307512/how-do-i-apply-orderby-on -an-iqueryable-a-string-column-name-in-a-gene [ ^ ]
问题是,当我在Customer类的原始属性上对它进行排序时,如'Name',它工作正常,但我怎样才能对CustSource的`Address.Street`属性进行排序?
Then I bind CustSource to a repeater.
Repeater have a column named 'Street' bound with `CustSource.address.Street`, 'House Number' bound with `CustSource.address.HouseNum`, 'Name' bound with `CustSource.Name` and so on.
I want to sort the the binding source i.e. CustSource at runtime. I know it could be done as CustSource.OrderBy(x=> x.Name) when I know the member at compile time. TO decide the member to sort on at runtime, I have written an extension method with the help of this article: http://stackoverflow.com/questions/307512/how-do-i-apply-orderby-on-an-iqueryable-using-a-string-column-name-within-a-gene[^]
Problem is, When I sort it on the primitive property of Customer class like 'Name', it works fine, but how can I sort on the `Address.Street` property of CustSource?
推荐答案
如果是关于各种整理顺序,你可以使用appr如对没有整理顺序的C#集合进行排序中所述[ ^ ]。
干杯
Andi
If it's about having various collating sequences, you might use the approach as described in Sorting C# collections that have no collating sequence[^].
Cheers
Andi
这篇关于排序列表< T>在< U>类型的成员上在运行时决定。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!