从IEnumerable转换<ienumerable <string>&gt;到ICollection <t> </t> </ienumerable <string>

时间:2012-11-07 19:30:26

标签: c# linq entity-framework casting

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

转换的原因是采用json反序列化并通过Entity Framework将其插入数据库。 json数据未规范化,与数据库的确切结构不匹配。在数据库中,我有movieperson,它们具有多对多关系。它们之间的桥接表是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> Characters添加MovieDataContract怎么办?然后我可以做这样的事情(理论上,没有测试过)......

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 }}
                }
            }
        } 
    }
}

2 个答案:

答案 0 :(得分:2)

我对我的回答做了一些假设:1。我们只是插入新电影,而不是更新数据库中的电影。 2.我们正在使用实体框架来执行插入操作。 3.正在添加的电影中的人不在与其他电影相关联的数据库中。每部电影都有一个演员阵容,每个演员至少有一个角色。

冒着被冷却的孩子巴士扔掉的风险,我的解决方案不使用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];
}

答案 1 :(得分: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