0

我有一个DataContract类,我正在尝试转换为另一个类,Movie但是对于该属性Movie.Characters,我收到了错误...

无法将源类型“ void”转换为目标类型“ ICollection<Character>

我怎样才能创建一个新MovieCharacter属性设置正确映射到每个PersonMovie

var movies = dataContract.Movies.Select(m => new Movie {
    Id = m.Id,
    Title = m.Title,
    Characters = m.AbridgedCast.ToList().ForEach(p => new Person { Name = p.Name, 
        RottenTomatoesId = p.Id, 
        Characters = p.Characters.ToList().ForEach(c => new Character { Name = c })})
});

MoviesDataContract.cs

public class MoviesDataContract {
    [DataMember(Name = "total")]
    public int Total { get; set; }

    [DataMember(Name = "movies")]
    public IEnumerable<Movie> Movies { get; set; }

    #region Internal Classes

    [DataContract]
    public class Movie {
        [DataMember(Name = "id")]
        public int Id { get; set; }

        [DataMember(Name = "title")]
        public string Title { get; set; }

        [DataMember(Name = "abridged_cast")]
        public virtual IEnumerable<Person> AbridgedCast { get; set; }
    }

    [DataContract]
    public class Person {
        [DataMember(Name = "name")]
        public string Name { get; set; }

        [DataMember(Name = "id")]
        public int Id { get; set; }

        [DataMember(Name = "characters")]
        public IEnumerable<string> Characters { get; set; }
    }

    #endregion  
}

电影.cs

public partial class Movie {
    public Movie() {
        this.Characters = new HashSet<Character>();
    }

    public int Id { get; set; }
    public string Title { get; set; }
    public virtual ICollection<Character> Characters { get; set; }
}

public partial class Character {
    public System.Guid Id { get; set; }
    public int MovieId { get; set; }
    public System.Guid PersonId { get; set; }
    public string Name { get; set; }

    public virtual Movie Movie { get; set; }
    public virtual Person Person { get; set; }
}

public partial class Person {
    public Person() {
        this.Characters = new HashSet<Character>();
    }

    public System.Guid Id { get; set; }
    public string Name { get; set; }
    public int RottenTomatoesId { get; set; }

    public virtual ICollection<Character> Characters { get; set; }
}
4

1 回答 1

3

List<T>.ForEach(...)不返回任何东西。请参阅 MSDN 上

你可能想用Select(...)then做一个投影ToList()

于 2012-11-06T21:23:12.660 回答