问题描述
我正在使用 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; }
}
是否有一种通用的方法来完成所有场景?
-
OrderBy(Client.Addresses.Where("Id = @0",idValue).CreatedDate + " ASC,LastName DESC,FirstName DESC")
-
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