0

嗨,我有两个包含属性的类,并且我有一个包含两个类的列表作为其属性的类。我应该返回一个列表,其中包含按 created_date 排序的两个类的对象。any1 有帮助吗?我已经粘贴了下面的 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,我应该根据创建日期显示最新的接触点或调查。

4

1 回答 1

2

听起来你需要Interface介于两者之间Classes

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();
于 2013-03-04T04:57:36.693 回答