简体   繁体   中英

sorting of two lists into one

Hi I have two classes which contains properties & i have a class which contains a list of the two classes as its property. I am supposed to return back a single list which contains the objects of both the classes sorted on created_date.Can any1 help? I have pasted the 3 classes below:

This is the first class:-

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; }
}

This is the second class:-

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; }
}

& this is the common class

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; }
}

To the UI i am supposed to show the latest touchpoints or surveys depending on the created date.

It sound like you are going to need an Interface between the two 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; }
}

Then you can create a single list IMyInterface

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM