0

我有一个使用动态 LINQ 执行排序的对象列表。

对象是这样的,

public class SampleDTO
    {
        public string Vendor { get; set;}
        public string Invoice { get; set; }
         ..
         ..

}

我使用 Dynamic Linq 库对此进行排序,

var list= new List<SampleDTO>();
list.OrderBy("Vendor");

如果我通过列表的有效属性名称(例如 Vendor )传递排序键,这很好用

问题是,如何对复杂对象执行此操作。

假设我有另一个对象,它是 SampleDTO 的属性

public class SampleDTO
    {
        public string Vendor { get; set;}
        public string Invoice { get; set; }
        public OtherDTO OtherDTO{get;set; }
         ..

}

public class OtherDTO 
{
        public string LineId{ get; set;}
        ..


}

如果我想让排序足够动态,以便我应该能够从 SampleDTO 的直接属性或 OtherDTO 的属性进行排序(例如,需要在 OtherDTO.LineId 上排序)

实现这一目标的可能方法是什么?

/BB

4

2 回答 2

4

为什么不使用 Lamba 语法。

list.OrderBy(sample => sample.OtherDto.LineId);

这具有不依赖硬编码字符串的优点

于 2010-11-25T11:44:55.190 回答
2

你可以这样做:

list.OrderBy("OtherDTO.LineId");
于 2010-11-25T11:41:59.390 回答