根据子列表的属性排序列表

时间:2018-10-17 13:34:07

标签: c# lambda

在下面的示例中,我有两个“空缺”的列表,并希望通过“ 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 :(得分:3)

怎么样: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);