Joining two lists of different types and sort them by date

c#, list

Solution

This problem could easily be solved by using polymorphism; use a common base class or interface for both classes, which has the `DateTime` property you want to sort on.

Example:

public abstract class NetworkingBase : EntityBase
{
    public DateTime DateToSortOn { get; set; }
}

or

public interface INetworking
{
    public DateTime DateToSortOn { get; set; }
}

And then make your classes derive from `NetworkingBase` or implement `INetworking`:

public partial class Networking : NetworkingBase
{
    ...
}

public partial class PrivateNetwork : NetworkingBase
{
    ...
}

or

public partial class Networking : EntityBase, INetworking
{
    ...
}

public partial class PrivateNetwork : EntityBase, INetworking
{
    ...
}

Do a LINQ `Union` or `Concat` and then an `OrderBy` on the resulting collection.

Problem

I have a first list of entities like this : ``` public partial class Networking :EntityBase { public virtual int NetWorkingId { get; set; } public virtual string NetWorkingParam { get; set; } public virtual System.DateTime NetWorkingDate { get; set; } } ``` And I have a second list of entities like this: ``` public partial class PrivateNetwork :EntityBase { public virtual int PrivateNetworkId { get; set; } public virtual int ContaId { get { return _contaId; } set { if (_contaId != value) { if (Contact != null && Contact.ContaId != value) { Contact = null; } _contaId = value; } } } public virtual Nullable<System.DateTime> DateCreation { get; set; } } ``` I want to collect these two lists in one and sort all the elements by date. Is that possible ?

Original source