对复杂对象使用动态 LINQ 进行排序

发布于 2024-10-04 04:42:23 字数 854 浏览 1 评论 0原文

我有一个对象列表,我使用动态 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

迷迭香的记忆 2024-10-11 04:42:23

为什么不使用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

慵挽 2024-10-11 04:42:23

你可以这样做:

list.OrderBy("OtherDTO.LineId");

You can do this:

list.OrderBy("OtherDTO.LineId");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文