'Order one list with a parameter from another list, with a joinable parameter

I have listA and listB.

ListA is a list of object which has an ListB object ID (foreignKey). And ListB is a list of object which has the property to OrderBy listA.

How can I order ListA by a ListB property with a simple Linq?

class OrderList
{
    public void OrderByDate(List<ProductionProgress> progressList, 
                            List<Production> productionList)
    {
        //Order progressList by productionList.ProductionDate
    }

    public class ProductionProgress
    {
        public int ProductionID { get; set; }
        public string Progress { get; set; }
    }

    public class Production
    {
        public int ID { get; }
        public string Description { get; set; }
        public DateTime ProductionDate { get; set; }
    }
}


Solution 1:[1]

You can use this query:

listA = listA
    .OrderBy(a => listb.FirstOrDefault(b => b.ID == a.objectB_id)?.Date ?? DateTime.MaxValue)
    .ToList();

This might not be the most efficient approach but it is safe. Because if you use Join it's possible that you get duplicate ObjectA instances if there are duplicate OBjectB(acc. to the ID).

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tim Schmelter