insanepaul
insanepaul

Reputation: 197

Ordering a list based on a property of a child list

I have a list of two 'Vacancies' in the example below and would like to order the list by 'DocumentTitle'. It's sufficient to order the document title in each vacancy then order the list by the first DocumentTitle of each vacancy. I tried many perforations of VacancyList.OrderBy(x => x.Documents.OrderBy(y => y.DocumentTitle))).ToList() to no avail

    namespace ConsoleApp1
            {
                class Program
                {
                    static void Main(string[] args)
                    {
                        List<Vacancy> vacancy = new List<Vacancy>
                        {
                            new Vacancy                    
                            {
                                Documents = new List<JobDocumentViewModel>
                                {
                                    new JobDocumentViewModel{DocumentTitle = "H",FileName="Somefile"},                        
                                    new JobDocumentViewModel{DocumentTitle = "A",FileName="Somefile"},
                                    new JobDocumentViewModel{DocumentTitle = "C",FileName="Somefile"}
                                }
                            },
                            new Vacancy
                            {
                                Documents = new List<JobDocumentViewModel>
                                {
                                    new JobDocumentViewModel{DocumentTitle = "Z",FileName="Somefile"},
                                    new JobDocumentViewModel{DocumentTitle = "B",FileName="Somefile"},
                                    new JobDocumentViewModel{DocumentTitle = "X",FileName="Somefile"}
                                }
                            }
                        };
    // What lambda expression would go here to order the vacancies by the document title
//Output - since the document title is "A" in the first vacancy then that would be first and the second vacancy document title of "B" would be next
                    }
                }
                public class Vacancy
                {
                    private List<JobDocumentViewModel> _documents;
                    public List<JobDocumentViewModel> Documents
                    {
                        get
                        {
                            if (_documents == null)
                                _documents = new List<JobDocumentViewModel>();

                            return _documents;
                        }
                        set { _documents = value; }
                    }
                }

                public class JobDocumentViewModel
                {
                    public string DocumentTitle { get; set; }
                    public string FileName { get; set; }

                    public JobDocumentViewModel() { }
                    public JobDocumentViewModel(
                        string documentTitle,
                        string fileName)
                    {
                        DocumentTitle = documentTitle;
                        FileName = fileName;
                    }
                }
            }

Upvotes: 0

Views: 58

Answers (1)

Antoine V
Antoine V

Reputation: 7204

How about this : https://dotnetfiddle.net/SzuuiF

vacancy.ForEach(x => x.Documents = x.Documents.OrderBy(y=> y.DocumentTitle).ToList());
var ordered = vacancy.OrderBy(x=> x.Documents.FirstOrDefault()?.DocumentTitle);

Upvotes: 3

Related Questions