【问题标题】:Dynamic Linq Order By Parent and Child Collection按父子集合的动态 Linq 顺序
【发布时间】:2021-06-07 13:42:21
【问题描述】:

我正在使用 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, LastName DESC, FirstName DESC")

【问题讨论】:

  • 试试.OrderBy(Client.Addresses.First("Id = @0", idValue).CreatedDate).ThenBy(LastName).ThenBy(FirstName")
  • @Emanuele First 只接受 1 个参数,我已经更新了我的问题

标签: c# dynamic-linq


【解决方案1】:

当使用System.Linq.Dynamic.Core时,你应该可以使用:

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

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

【讨论】:

  • 谢谢,我已验证方案 2 有效。您知道动态 linq 是否也支持场景 1?
猜你喜欢
  • 1970-01-01
  • 2019-02-12
  • 2021-11-28
  • 2016-05-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-29
  • 1970-01-01
相关资源
最近更新 更多