Note that there are some explanatory texts on larger screens.

plurals
  1. POConverting from IEnumerable<IEnumerable<string>> to ICollection<T>
    text
    copied!<p>I'm trying to convert an <code>IEnumerable&lt;IEnumerable&lt;string&gt;&gt;</code> to <code>ICollection&lt;Character&gt;</code> but struggling to figure out the right way to do it since the structure of one object is different than the structure of the other.</p> <p>The reason for conversion is to take a json deserialization and insert that into a database via Entity Framework. The json data isn't normalized and doesn't match the exact structure of the database. In the database I have a <code>movie</code> and a <code>person</code> that have a many-to-many relationship. The bridge table between them is <code>characters</code>. But the json just has a movie object with an array of people objects and each person with an array of the characters they play (<code>IEnumerable&lt;IEnumerable&lt;string&gt;&gt;</code>).</p> <p>Basically, I need to convert each movie's people and their characters to each movie's character and its respective person.</p> <pre><code>var movie = dataContractMovies.Select(m =&gt; 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 }); </code></pre> <p><strong>Convert From</strong></p> <pre><code>public class MovieDataContract { [DataMember(Name = "title")] public string Title { get; set; } [DataMember(Name = "abridged_cast")] public virtual IEnumerable&lt;Person&gt; Cast { get; set; } } [DataContract] public class Person { [DataMember(Name = "name")] public string Name { get; set; } [DataMember(Name = "characters")] public IEnumerable&lt;string&gt; Characters { get; set; } } </code></pre> <p><strong>Convert To</strong></p> <pre><code>public partial class Movie { public string Title { get; set; } public virtual ICollection&lt;Character&gt; Characters { get; set; } public Movie() { this.Characters = new HashSet&lt;Character&gt;(); } } 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&lt;Character&gt; Characters { get; set; } public Person() { this.Characters = new HashSet&lt;Character&gt;(); } } </code></pre> <p><strong>UPDATE AND FURTHER QUESTION</strong></p> <p>What if I just added a <code>public IEnumerable&lt;Character&gt; Characters</code> to the <code>MovieDataContract</code>? Then I could just do something like this (in theory, haven't tested)...</p> <pre><code>Characters = m.Characters; </code></pre> <p>So my new <code>MovieDataContract</code> would look like this...</p> <pre><code>[DataContract] public class MovieDataContract { [DataMember(Name = "title")] public string Title { get; set; } [DataMember(Name = "abridged_cast")] private IEnumerable&lt;Person&gt; _cast { get; set; } public IEnumerable&lt;Character&gt; 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 }} } } } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload