title: Pass a column value to model from a different table based on foreign key id
body: <p>I have a <code>products</code> table which has a <code>CategoryId</code> which represents the respective primary key from the <code>Categories</code> table.</p>
  
  <p><strong>ProductViewModel</strong></p>
  
  <pre><code>public ProductVM(ProductDTO productDTO)
  {
      Id = productDTO.Id;
      Name = productDTO.Name;
      Description = productDTO.Description;
      Price = productDTO.Price;
      CategoryId = productDTO.CategoryId;
      ImageName = productDTO.ImageName;
  }
  
  public int Id { get; set; }
  [Required]
  public string Name { get; set; }
  [Required]
  public string Description { get; set; }
  [Required]
  public decimal Price { get; set; }
  
  public int? CategoryId { get; set; }
  public IEnumerable&lt;SelectListItem&gt; Categories { get; set; }
  public string ImageName { get; set; }
  public IEnumerable&lt;string&gt; GalleryImages { get; set; }
  </code></pre>
  
  <p><strong>Product DTO</strong></p>
  
  <pre><code>public class ProductDTO
  {
      [Key]
      public int Id { get; set; }
      public string Name { get; set; }
      public string Slug { get; set; }
      public string Description { get; set; }
      public decimal Price { get; set; }
      public int CategoryId { get; set; }
      public string ImageName { get; set; }
  
      [ForeignKey("CategoryId")]
      public virtual CategoryDTO Category { get; set; }
  }
  </code></pre>
  
  <p>This is how I get a list of products:</p>
  
  <pre><code>List&lt;ProductVM&gt; productVM;
  using (Db db = new Db())
  {
      productVM = db.Products
          .ToArray()
          .Select(x =&gt; new ProductVM(x))
          .ToList();
  }
  </code></pre>
  
  <p>As you can see I am passing the <code>CategoryId</code> around and I can display it in my views with the <code>ProductVM</code> ViewModel, but I also want to get the <code>Name</code> of the category in there as well.</p>
  
  <p>I can think of some hacks, e.g. accessing the <code>DB</code> from the constructor in the <code>ViewModel</code> based on the <code>CategoryId</code> and assigning it that way, but I wanna see if there is a more elegant solution?</p>
  
  <p>Bottom line - I have a <code>Name</code> column in my <code>Categories</code> table and I want to pass that name to the <code>ProductVM</code> in a most effective way.</p>