对复杂对象使用动态 LINQ 进行排序
我有一个对象列表,我使用动态 LINQ 对其执行排序。
对象是这样的,
public class SampleDTO
{
public string Vendor { get; set;}
public string Invoice { get; set; }
..
..
}
我使用动态 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 的属性上进行排序(例如需要排序on OtherDTO.LineId )
实现此目标的可能方法有哪些?
/BB
I have a list of Objects on which I use dynamic LINQ to perform sorting.
The object is like this,
public class SampleDTO
{
public string Vendor { get; set;}
public string Invoice { get; set; }
..
..
}
And I use Dynamic Linq library to sort this,
var list= new List<SampleDTO>();
list.OrderBy("Vendor");
This works fine if I pass a sort key with a valid property name of the list ( e.g. Vendor )
The problem is, how to do this for a complex object.
Assume I have a another object which is a property of the 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;}
..
}
And if I want to make the sorting dynamic enough so that I should be able to sort from a direct property of the SampleDTO or on a property of a OtherDTO ( e.g need to sort on OtherDTO.LineId )
What are the possible ways of achieving this?
/BB
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么不使用lambda语法呢?
list.OrderBy(sample => sample.OtherDto.LineId);
这具有不依赖于硬编码字符串的优点
why not use the lamba syntax.
list.OrderBy(sample => sample.OtherDto.LineId);
This has the advantage of not being reliant on hard coded strings
你可以这样做:
You can do this: