Assuming I have Customer and Order objects, where one Customer can have many Orders (so the Order class has a CustomerId property), and I want to return a collection of all CustomerAndMostRecentOrder objects which are defined as follows:
public class CustomerAndMostRecentOrder
{
public Customer Customer { get; set; }
public Order MostRecentOrder { get; set; }
}
How would I write a Linq query which does this (I’m using Linq to SQL)?
You will need to have an
CreatedDatedate in yourOrdertable to get the most recent order. Then to get yourCustomerAndMostRecentOrderobject, do the following query: