Упорядочение списка на основе свойства дочернего списка - PullRequest
0 голосов
/ 17 октября 2018

У меня есть список из двух «Вакансий» в приведенном ниже примере, и я хотел бы упорядочить список по «DocumentTitle».Достаточно заказать заголовок документа в каждой вакансии, а затем упорядочить список по первому DocumentTitle каждой вакансии.Я пробовал много перфораций VacancyList.OrderBy(x => x.Documents.OrderBy(y => y.DocumentTitle))).ToList() безрезультатно

    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;
                    }
                }
            }

1 Ответ

0 голосов
/ 17 октября 2018

Как насчет этого: 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);
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...