将两个列表排序为一个列表

本文关键字:列表 一个 两个 排序 | 更新日期: 2023-09-27 18:01:55

嗨,我有两个类,其中包含属性&我有一个类,其中包含两个类的列表作为其属性。我应该返回一个单一的列表,其中包含两个类的对象在created_date上排序。有人能帮忙吗?我已经粘贴了下面的3个类:

这是第一堂课:-

public partial class TouchPointModel : BaseModel
{
    public List<EntityModel> Entities { get; set; }
    public UserModel User { get; set; }
    public int TouchPointId { get; set; }
    public string Description { get; set; }
    public int EntityId { get; set; }
    public DateTime Created_date{ get;set; }
}

这是第二类:-

public partial class SurveyModel : BaseModel
{
    public int Id { get; set; }
    public int SelectedEntityId { get; set; }
    public int SelectedEntityType { get; set; }
    public int ParentEntityId { get; set; }
    public bool IsMyProjects { get; set; }
    public DateTime Created_date{ get;set; }
}

,这是一个常见的类

public partial class RecentInteractionsModel
{
    public int ContactId { get; set; }
    public List<TouchPointModel> TouchPoints { get; set; }
    public List<SurveyModel> Surveys { get; set; }
    public int EntityId { get; set; }
    public int EntityTypeId { get; set; }
    public int SelectedParentId { get; set; }
}

对于UI,我应该根据创建的日期显示最新的接触点或调查。

将两个列表排序为一个列表

听起来您需要在两个Classes之间添加一个Interface

public interface IMyInterface
{
   DateTime Created_date{ get;set; }
   // add any other common properties between the 2 models
}

public partial class TouchPointModel : BaseModel, IMyInterface
{
   .......
   public DateTime Created_date{ get;set; }
}
public partial class SurveyModel : IMyInterface
{
   .......
   public DateTime Created_date{ get;set; }
}

然后你可以创建一个单独的列表IMyInterface

List<IMyInterface> allItems = TouchPoints.Cast<IMyInterface>()
                       .Union(Surveys.Cast<IMyInterface>())
                       .OrderBy(x => x.Created_date).ToList();