Note that there are some explanatory texts on larger screens.

plurals
  1. POAutomapper and Linq with joined tables, populating ViewModel properties
    text
    copied!<p>Having just figured out how to populate my ViewModel from a model using Automapper, I am now onto the next challenge – populating the ViewModel properties from a joined table. </p> <p>The image below depicts my simple database. </p> <p><img src="https://i.stack.imgur.com/1oyTv.png" alt="enter image description here"></p> <p>My ViewModel class is defined as: </p> <pre><code>public class PersonViewModel { public int Id { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public Nullable&lt;int&gt; Age { get; set; } public Nullable&lt;int&gt; SuffixId { get; set; } public string PersonStatus { get; set; } public string PersonPreference { get; set; } public virtual Suffix Suffix { get; set; } } enter code here </code></pre> <p>Note the additional fields of PersonStatus and PersonPreference that will come from joining PersonInfo to Person. Here is the code I use to instantiate my mapping configuration: </p> <pre><code> Mapper.CreateMap&lt;PersonViewModel, Person&gt;(); Mapper.CreateMap&lt;Person, PersonViewModel&gt;(); </code></pre> <p>And now the code to populate the ViewModel</p> <pre><code> List&lt;PersonViewModel&gt; persons = null; using (var context = new DemoEntities()) { persons = (from p in context.People.AsEnumerable() join i in context.PersonInfoes on p.Id equals i.PersonId select Mapper.Map&lt;PersonViewModel&gt;(p)).ToList(); return persons; } </code></pre> <p>How would I populate the two joined properties (PersonStatus and PersonPreference) from this setup? </p> <p>Thanks in advance! </p>
 

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