5

我正在尝试将其转换IEnumerable<IEnumerable<string>>ICollection<Character>但努力找出正确的方法,因为一个对象的结构与另一个对象的结构不同。

转换的原因是进行 json 反序列化并通过 Entity Framework 将其插入数据库。json 数据未规范化,与数据库的确切结构不匹配。在数据库中,我有一个movie和一个person具有多对多关系的。他们之间的桥梁表是characters。但是 json 中只有一个包含人物对象数组的电影对象,每个人都有他们扮演的角色数组(IEnumerable<IEnumerable<string>>)。

基本上,我需要将每部电影的人物及其角色转换为每部电影的角色及其各自的人物。

var movie = dataContractMovies.Select(m => new Movie {
    Title = m.Title,
    // not sure how to convert each Person and their characters
    Characters = // Need to take each Person and their Characters in m.Cast and cast to a new Character and a new Person for which the Character belongs to
});

转换自

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

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

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

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

转换成

public partial class Movie {
    public string Title { get; set; }
    public virtual ICollection<Character> Characters { get; set; }

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

public partial class Character {
    public string Name { get; set; }
    public virtual Movie Movie { get; set; }
    public virtual Person Person { get; set; }
}

public partial class Person {
    public string Name { get; set; }
    public virtual ICollection<Character> Characters { get; set; }

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

更新和进一步的问题

如果我只是public IEnumerable<Character> CharactersMovieDataContract? 然后我可以做这样的事情(理论上,没有测试过)......

Characters = m.Characters;

所以我的新MovieDataContract产品看起来像这样......

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

    [DataMember(Name = "abridged_cast")]
    private IEnumerable<Person> _cast { get; set; }

    public IEnumerable<Character> Characters { 
        get{  
            foreach(var person in _cast) {
                foreach(string name in person.characters) {
                    yield return new Character { Name = name, Person = new Person { Name = person.name }}
                }
            }
        } 
    }
}
4

2 回答 2

2

我为我的回答做了一些假设: 1. 我们只是插入新电影,而不是更新数据库中已经存在的电影。2. 我们使用实体框架进行插入。3.正在添加的电影中的人尚未在与其他电影关联的数据库中。4. 每部电影都有演员,每个演员至少有一个角色。

冒着被赶出酷儿童巴士的风险,我的解决方案不使用 LINQ,尽管我确信它可以被修改为这样做。请注意,使用 EF,您无需设置关联的两端即可将其插入数据库。

// Lookup is necessary because a person might be in more than one movie.
var personLookup = new Dictionary<string,Person>();

foreach (var contractMovie in dataContractMovies)
{
    var movie = new Movie() { Title = contractMovie.Title };

    foreach (var contractPerson in contractMovie.Cast)
    {
        if (!personLookup.ContainsKey(contractPerson.Name))
        {
            personLookup.Add(contractPerson.Name, new Person() { Name = contractPerson.Name });
        }
        var person = personLookup[contractPerson.Name];

        foreach (var contractCharacter in contractPerson.Characters)
        {
            var character = new Character() { Name = contractCharacter.Name, Person = person, Movie = movie };

            dataContext.Characters.Add(character);
        }
    }
 }
 dataContext.SaveChanges();

如果 DataContract 将 Movie 映射到 Character 而不是 Person(就像您在评论中建议的那样),您可以摆脱类似这样的事情:

var personLookup = new Dictionary<string, Person>();
var movie = dataContractMovies.Select(m => new Movie {
    Title = m.Title,
    Characters = m.Characters.Select(c => new Character() { Name = c.Name, Person = LookupPerson(c.Person, personLookup) }).ToList()
});

public Person LookupPerson(string personName, Dictionary<string, Person> personLookup)
{
        if (!personLookup.ContainsKey(personName))
        {
            personLookup.Add(personName, new Person() { Name = personName });
        }
        return personLookup[personName];
}
于 2012-11-07T22:24:59.690 回答
1
var flattenedShape =
  from movie in dataContractMovies
  from person in movie.Cast
  from characterName in person.Characters
  select new {Movie = movie, Person = person, CharacterName = characterName};


List<Character> characters = new List<Character>();
Dictionary<MovieDataContract, Movie> movieMap =
  new Dictionary<MovieDataContract, Movie>();
Dictionary<PersonDataContract, Person> personMap =
  new Dictionary<PersonDataContract, Person>();

foreach(var row in flattenedShape)
{
  //have I seen this movie yet?
  if (!movieMap.ContainsKey(row.Movie))
  {
    movieMap.Add(row.Movie, new Movie() { Title = row.Movie.Title });
  }

  //have I seen this person yet?
  if (!personMap.ContainsKey(row.Person))
  {
    personMap.Add(row.Person, new Person() { Name = row.Person.Name });
  }

  //every character is unique.
  Character x = new Character() { Name = row.CharacterName };
  movieMap.Characters.Add(x);
  x.Movie = movieMap[row.Movie];
  personMap.Characters.Add(x);
  x.Person = personMap[row.Person];
  characters.Add(x);
}

//at this point, all the characters are in characters...
// all the movies are in the movieMap.Values...
// and all the persons are in the personMap.Values
于 2012-11-07T21:15:43.050 回答