-4
class A
{
   int id,
   DateTime joinDate,
   string name
}

class B
{
   int uniqueId,
   DateTime joinDate,
}

I have two lists with the above objects

List<A> listofA
List<B> listofB

I want to
merge these two lists into a new list and then sort the new list by DateTime
which is common to both lists.

I have followed the this link to understand the merge logic.

Kindly do provide a feasible solution to understand such sorting logic

So far the result

List<object> mergedList = (from x in A select (Object)x).ToList();
mergedList.AddRange((from x in B select (Object)x).ToList());

mergedList.Orderby()??? based on joinDate

Karan Ginimav
  • 35
  • 1
  • 10

3 Answers3

1

Something like this could work:

var m = A.Select(a => new { a.JoinDate, O = (object)a })
  .Concat(B.Select(b => new { b.JoinDate, O = (object)b }))
  .OrderBy(c => c.JoinDate);

But note that for each item in the list you're then going to have to ask if O is a ClassA or a ClassB

for(var x in m){
  if(x.O is ClassA)
    ...
}

I do think the suggestions in the comments that these classes should descend from a common ancestor or implement a common interface would make them easier to work with..

Caius Jard
  • 72,509
  • 5
  • 49
  • 80
0

As noted you can't merge two disparate class types without resorting to using the object class, which I'd recommend against as it is a potential source of introducing bugs. Better to give each class a base interface and work with a list based on that.

You then need to compare your property type in order to determine how best to sort it.

using System;
using System.Collections.Generic;
using System.Linq;
                
public class Program
{
    public static void Main()
    {
        List<ClassA> listA = new List<ClassA>()
        { 
            new ClassA(){Id=1, JoinDate = new DateTime(2013, 6, 1, 12, 32, 30)},
            new ClassA(){Id=2, JoinDate = new DateTime(2021, 7, 2, 12, 32, 30)},
        };
        List<ClassB> listB = new List<ClassB>()
        {
            new ClassB(){UniqueId=23, JoinDate = new DateTime(2021, 2, 6, 8, 32, 30)}, 
            new ClassB(){UniqueId=47, JoinDate = new DateTime(2017, 2, 6, 8, 32, 30)}, 
        };
        
        List<IJoinDate> listJoinDate = new List<IJoinDate>();
        
        foreach(var item in listA)
        {
            listJoinDate.Add(item);
        }
            
        foreach(var item in listB)
        {
            listJoinDate.Add(item);
        }
        
        listJoinDate.Sort((x,y)=> 
                          {
                            if (x.JoinDate == null && y.JoinDate == null) return 0;
                            else if (x.JoinDate == null) return -1;
                            else if (y.JoinDate == null) return 1;
                            else return x.JoinDate.CompareTo(y.JoinDate);
                          });
        Console.WriteLine("Total records = %d", listJoinDate.Count);
        foreach(var item in listJoinDate)
        {
            Console.WriteLine(item.JoinDate);
        }
        Console.WriteLine("Linq OrderByDescending");
        listJoinDate.AsQueryable().OrderByDescending(x=>x.JoinDate);
        foreach(var item in listJoinDate)
        {
            Console.WriteLine(item.JoinDate);
        }
    
        Console.WriteLine("Linq OrderBy");
        listJoinDate.AsQueryable().OrderBy(x=>x.JoinDate);
        foreach(var item in listJoinDate)
        {
            Console.WriteLine(item.JoinDate);
        }
    }
}
public interface IJoinDate
{
    DateTime JoinDate{get;set;}
}

public class ClassA :IJoinDate
{
    public int Id{get;set;}
    public DateTime JoinDate{get;set;}
}
public class ClassB :IJoinDate
{
    public int UniqueId{get;set;}
    public DateTime JoinDate{get;set;}
    public string Name{get;set;}
}

 
ChrisBD
  • 9,104
  • 3
  • 22
  • 35
0

Populate your lists, listofA and listofB and use following queries.

Order by Ascending

var mergedList = (from elementA in listofA select new { ID = elementA.id, JoinDate = elementA.joinDate, Name = elementA.name })
.Concat(from elementB in listofB select new { ID = elementB.uniqueId, JoinDate = elementB.joinDate, Name = string.Empty })
.OrderBy(x => x.JoinDate);

Order by Descending

var mergedList = (from elementA in listofA select new { ID = elementA.id, JoinDate = elementA.joinDate, Name = elementA.name })
.Concat(from elementB in listofB select new { ID = elementB.uniqueId, JoinDate = elementB.joinDate, Name = string.Empty })
.OrderByDescending(x => x.JoinDate);
NCCSBIM071
  • 1,207
  • 1
  • 16
  • 30