Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If two or more <code>student in _xStudents.Descendants("Student")</code> reference the same parent (by id), you then create two or more <code>ParentDTO</code>s with the same id, so you are then trying to insert the same Primary Key twice within your <code>Importer</code> class.</p> <p>If you simply pre-process your <code>_xParents</code>, to transform them into a new list of ParentDTO, which is unique by <code>ParentId</code> you can then use that in your <code>var query</code> to get a reference to the single <code>ParentDTO</code> instance that refers to the given ParentId PK.</p> <p>This code sample doesn't change your code much so that you can easily relate it to your original. Note, however, that you can probably optimise this, and you will also have the same problem with your <code>SubjectDTO</code> list as well if you are using <code>SubjectDTO.Name</code> to be unique (as you should be, I guess). </p> <pre><code>var parents = (from parent in _xParents.Descendants("Parent").DefaultIfEmpty() group parent by (String)parent.Element("ParentId") into pg select new ParentDTO { ParentId = (long)pg.FirstOrDefault().Element("ParentId"), Name = (String)pg.FirstOrDefault().Element("Name") // you might want to not use ToList here and let parents be an IEnumerable instead }).ToList(); var query = from student in _xStudents.Descendants("Student") select new StudentDTO { StudentId = (long)student.Element("StudentId"), Name = (String)student.Element("Name"), Subjects = ( from subject in _xSubjects.Descendants("Subject").DefaultIfEmpty() where (String)student.Element("StudentId") == (String)subject.Element("StudentId") select new SubjectDTO { Name = (String)subject.Element("Name") } ).ToList(), Parents = ( from parent in parents // Calling ToString each time is not fantastic where (String)student.Element("Parent1") == parent.ParentId.ToString() || (String)student.Element("Parent2") == parent.ParentId.ToString() || (String)student.Element("Parent3") == parent.ParentId.ToString() select parent ).ToList() }; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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