1

这个可以改吗...

var movies = rawMovies.Movies.Select(m => {
    var movie = new Movie {
        Id = m.Id,
        Title = m.Title,
        Year = m.Year,
        MpaaRating = m.MpaaRating,
        Runtime = m.Runtime,
        CriticsConsensus = m.CriticsConsensus,
        Synopsis = m.Synopsis,
        ReleaseDate = m.ReleaseDates != null ? new ReleaseDate { Dvd = m.ReleaseDates.Dvd, Theater = m.ReleaseDates.Theater } : null,
        Rating = m.Ratings != null ? new Rating { AudienceScore = m.Ratings.AudienceScore, CriticsScore = m.Ratings.CriticsScore } : null,
        AlternateId = m.AlternateIds != null ? new AlternateId { Imdb = m.AlternateIds.Imdb } : null,
        Poster = m.Posters != null ? new Poster { Detailed = m.Posters.Detailed, Original = m.Posters.Original, Profile = m.Posters.Profile, Thumbnail = m.Posters.Thumbnail } : null,
        Link = m.Links != null ? new Link { Alternate = m.Links.Alternate, Cast = m.Links.Cast, Clips = m.Links.Clips, Reviews = m.Links.Reviews, Self = m.Links.Self, Similar = m.Links.Similar } : null,
        Genres = m.Genres != null ? (HashSet<Genre>)m.Genres.Select(g => new Genre { Name = g }) : new HashSet<Genre>()
    };

    foreach (var castMember in m.AbridgedCast) {
        var person = new Person { Id = castMember.Id, Name = castMember.Name };
        if (castMember.Characters != null) {
            foreach (string name in castMember.Characters) {
                var character = new Character { Name = name, Person = person, Movie = movie };
                movie.Characters.Add(character);
            }
        }
    }

    return movie;                           
});

return movies;

foreach(var rawMovie in rawMovies.Movies) { ... }yield return?_

4

1 回答 1

1

是的。它是按照你开始的方式完成的......

foreach(var rawMovie in rawMovies.Movies)
{
  var m = rawMovie;
    var movie = new Movie {
        Id = m.Id,
        Title = m.Title,
        Year = m.Year,
        MpaaRating = m.MpaaRating,
        Runtime = m.Runtime,
        CriticsConsensus = m.CriticsConsensus,
        Synopsis = m.Synopsis,
        ReleaseDate = m.ReleaseDates != null ? new ReleaseDate { Dvd = m.ReleaseDates.Dvd, Theater = m.ReleaseDates.Theater } : null,
        Rating = m.Ratings != null ? new Rating { AudienceScore = m.Ratings.AudienceScore, CriticsScore = m.Ratings.CriticsScore } : null,
        AlternateId = m.AlternateIds != null ? new AlternateId { Imdb = m.AlternateIds.Imdb } : null,
        Poster = m.Posters != null ? new Poster { Detailed = m.Posters.Detailed, Original = m.Posters.Original, Profile = m.Posters.Profile, Thumbnail = m.Posters.Thumbnail } : null,
        Link = m.Links != null ? new Link { Alternate = m.Links.Alternate, Cast = m.Links.Cast, Clips = m.Links.Clips, Reviews = m.Links.Reviews, Self = m.Links.Self, Similar = m.Links.Similar } : null,
        Genres = m.Genres != null ? (HashSet<Genre>)m.Genres.Select(g => new Genre { Name = g }) : new HashSet<Genre>()
    };

    foreach (var castMember in m.AbridgedCast) {
        var person = new Person { Id = castMember.Id, Name = castMember.Name };
        if (castMember.Characters != null) {
            foreach (string name in castMember.Characters) {
                var character = new Character { Name = name, Person = person, Movie = movie };
                movie.Characters.Add(character);
            }
        }
    }

    yield return movie;
}

确保您的方法返回IEnumerable<Movie>并且一切就绪。

于 2012-11-30T20:41:20.570 回答