如何通过子子对象来命令父对象

时间:2015-02-10 04:37:19

标签: c# linq

我有一个List< PaperAbstract>类。 PaperAbstract类有一组作者。其中一位作者有一个标志IsSubmitting为true。我如何订购我的清单< PaperAbstract>由提交作者LastName?

public class PaperAbstract
{
    public string Title { get; set; }
    public List<Author> Authors { get; set; }
}

public class Author
{
    public bool IsSubmitting { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

例如:

var paperAbstracts = new List<PaperAbstract>();
paperAbstracts.Add(new PaperAbstract
{
    Title = "Abstract 2",
    Authors = new List<Author>
    {
        new Author { IsSubmitting = false, FirstName = "F5", LastName = "L5"},
        new Author { IsSubmitting = true, FirstName = "F6", LastName = "L6"}
    }
});
paperAbstracts.Add(new PaperAbstract
{
    Title = "Abstract 3",
    Authors = new List<Author>
    {
        new Author { IsSubmitting = true, FirstName = "F1", LastName = "L1"},
        new Author { IsSubmitting = false, FirstName = "F2", LastName = "L2"}
    }
});
paperAbstracts.Add(new PaperAbstract
{
    Title = "Abstract 1",
    Authors = new List<Author>
    {
        new Author { IsSubmitting = false, FirstName = "F3", LastName = "L3"},
        new Author { IsSubmitting = true, FirstName = "F4", LastName = "L4"}
    }
});

正确的论文顺序摘要应该是摘要3,摘要1,摘要2。

1 个答案:

答案 0 :(得分:1)

您可以使用LINQ中的OrderBy

 var result = input.OrderBy(x => x.Authors
                                  .First(a => a.IsSubmitting).LastName)