订购/排序嵌套通用列表

时间:2012-02-16 21:55:13

标签: c# list generics sorting sql-order-by

我有两个类似的实体:

public class Movie
{
    public int MovieID { get; set; }

    public string Title { get; set; }

    public List<Director> Directors { get; set; };

    public int Year { get; set; }
}

public class Director
{
    public int DirectorID { get; set; }

    public string Name { get; set; }

    public List<Movie> Movies { get; set; }
}

假设我想以导演的名义订购电影列表:

Movies.OrderBy (??? => ???)

我该怎么做?

提前致谢。

1 个答案:

答案 0 :(得分:0)

var orderedMovies =
    movies.OrderBy(movie => 
                    String.Join(",", movie.Directors
                                             .OrderBy(dir => dir.Name)
                                             .Select(dir => dir.Name)
                    )
    );