I have a list with objects such as this one:
public class ReportViewModel
{
public string DocumentName { get; set; } = string.Empty;
public DateTime? DocumentDate { get; set; }
public string DocumentTypeName { get; set; } = string.Empty;
}
I want to get distinct list of objects, where I will get the newest document based on the document type, and only get the latest document for each type. The api currently returns list with documents and it might documents from the same document type but from a different date, how can I get only the latest document from each document type?
You need to use
GroupByandFirstOrDefaulttogether.Let's say you have
Then what you need to do is
It gives you the following result as expected.
See: How to get first record in each group using Linq