微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

按父子集合的动态 Linq 顺序

如何解决按父子集合的动态 Linq 顺序

我正在使用 System.Linq.Dynamic。

鉴于下面的这个父 - 子(集合)模型,是否可以使用基于 createdDate 属性的 OrderBy Client 传递特定地址的 Id 进行排序?或者,客户端属性也可用于主排序。它还必须按姓氏和名字排序。

public class Client
{
    public int Id { get; set; }
    public string FirstName{ get; set; }
    public string LastName { get; set; }
    public string Description {get; set;}
    public string Type {get; set;}
    public virtual ICollection<Address> Addresses { get; set; }
}

public class Address
{
    public int Id { get; set; }
    public DateTime createdDate { get; set; }
    public DateTime modifiedDate { get; set; }
    public string AddressLine1 { get; set; }
    public string AddressLine2 { get; set; }
    public string AddressLine3 { get; set; }
}

是否有一种通用的方法来完成所有场景?

  1. OrderBy(Client.Addresses.Where("Id = @0",idValue).CreatedDate + " ASC,LastName DESC,FirstName DESC")

  2. OrderBy("Type DESC,FirstName DESC")

解决方法

使用 System.Linq.Dynamic.Core 时,您应该能够使用:

.OrderBy("Type DESC,LastName DESC,FirstName DESC")

另见:https://dynamic-linq.net/basic-simple-query#ordering-results

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。