title: Accessing data in a ViewModel
body: <p>I have the following entity framework code snippet which has a "Groups" table and a child "ApplicationsGroupsLK" table that contains an ApplicationID field that I need.</p>
  
  <pre><code>IEnumerable&lt;Groups&gt; Groups = DbContext.Groups.Include("ApplicationsGroupsLK").Where(p =&gt; p.GroupNumber &gt; 0);
  </code></pre>
  
  <p>The child data comes back obviously in a collection. </p>
  
  <p>I basically need to display the parent data along with the child ApplicationID field (many Applications to 1 Group).</p>
  
  <h2>In my MVC View, what should my ViewModel look like that would contain the parent and child data coming back that I need in order that I can properly bind it to a grid?</h2>
  
  <p>Second post:</p>
  
  <p>Further, the following model was generated from Entity Framework:</p>
  
  <pre><code>public partial class Project
      {
          public Project()
          {
              this.TimeTrackings = new HashSet&lt;TimeTracking&gt;();
          }
  
       [DataMember]
          public short ProjectID { get; set; }
       [DataMember]
          public short CustomerID { get; set; }
       [DataMember]
          public string Name { get; set; }
       [DataMember]
          public string Description { get; set; }
       [DataMember]
          public short CategoryID { get; set; }
       [DataMember]
          public short PriorityID { get; set; }
       [DataMember]
          public short StatusID { get; set; }
       [DataMember]
          public Nullable&lt;decimal&gt; Quote { get; set; }
       [DataMember]
          public string Notes { get; set; }
       [DataMember]
          public System.DateTime CreatedDate { get; set; }
       [DataMember]
          public Nullable&lt;System.DateTime&gt; UpdatedDate { get; set; }
  
       [DataMember]
          public virtual Category Category { get; set; }
       [DataMember]
          public virtual Customer Customer { get; set; }
       [DataMember]
          public virtual Priority Priority { get; set; }
       [DataMember]
          public virtual Status Status { get; set; }
       [DataMember]
          public virtual ICollection&lt;TimeTracking&gt; TimeTrackings { get; set; }
      }
  </code></pre>
  
  <p>You can see that TimeTrackings is the child table of Project. You can also see that CategoryID, CustomerID, PriorityID, and StatusID are foreign keys that the parent table has. In this case, I'm only interested in the CategoryID FK.</p>
  
  <h2>I haven't done this yet (not at my machine at home), but when I get the data into this model, <strong>what would actually be contained in the public virtual Category Category field</strong>? Since it's not a collection, what data is returned in this field after the query executes.</h2>
  
  <p>Third post:
  Telerik asp.net for mvc syntax for DB call:</p>
  
  <pre><code>IEnumerable&lt;Groups&gt; GroupList = db.GetGroups();
  
              return View(new GridModel&lt;Groups&gt;
              {
                  Data = GroupList
              });
  </code></pre>
  
  <hr>
  
  <p>Fourth post:
  Trey, below is the code I modified for my purposes and was hoping you can check it over before I implement it. I think I undersand it and seems great...</p>
  
  <pre><code>public partial class Project
      {
          public Project()
          {
              this.TimeTrackings = new HashSet&lt;TimeTracking&gt;();
          }
  
       [DataMember]
          public short ProjectID { get; set; }
       [DataMember]
          public short CustomerID { get; set; }
       [DataMember]
          public string Name { get; set; }
       [DataMember]
          public string Description { get; set; }
       [DataMember]
          public short CategoryID { get; set; }
       [DataMember]
          public short PriorityID { get; set; }
       [DataMember]
          public short StatusID { get; set; }
       [DataMember]
          public Nullable&lt;decimal&gt; Quote { get; set; }
       [DataMember]
          public string Notes { get; set; }
       [DataMember]
          public System.DateTime CreatedDate { get; set; }
       [DataMember]
          public Nullable&lt;System.DateTime&gt; UpdatedDate { get; set; }
       [DataMember]
          public short ApplicationID { get; set; }
       [DataMember]
          public string ApplicationName { get; set; }
       [DataMember]
          public virtual Category Category { get; set; }
       [DataMember]
          public virtual Customer Customer { get; set; }
       [DataMember]
          public virtual Priority Priority { get; set; }
       [DataMember]
          public virtual Status Status { get; set; }
       [DataMember]
          public virtual ICollection&lt;TimeTracking&gt; TimeTrackings { get; set; }
  
  
       public ProjectModel(Project project)
       {
          ProjectID = project.ProjectID;
          CustomerID = project.CustomerID;
          Name = project.Name;
          Description = project.Description;
      CategoryID = project.CategoryID;
          PriorityID = project.PriorityID;
          StatusID = project.StatusID;
          Quote = project.Quote;
          Notes = project.Notes;
          CreatedDate = project.CreatedDate;
          UpdatedDate = project.UpdatedDate;
          ApplicationID = project.ApplicationsGroupsLK.ApplicationID;
          ApplicationName = project.ApplicationsGroupsLK.ApplicationName;
       }
  
  
       // Neat Linq trick to convert database query results directly to Model
       public static IList&lt;ProjectModel&gt; FlattenToThis(IList&lt;Project&gt; projects)
       {
          return projects.Select(project =&gt; new ProjectModel(project)).ToList();
       }
  
      }
  </code></pre>
  
  <hr>
  
  <p>Fifth post:</p>
  
  <pre><code>using (wmswebEntities DbContext = new wmswebEntities())
                  {
                      DbContext.Configuration.ProxyCreationEnabled = false;
                      DbContext.Database.Connection.Open();
  
                      IEnumerable&lt;Projects&gt; projects = DbContext.Projects.Where(p =&gt; p.GroupNumber &gt; 0);
  
                      IList&lt;ProjectModel&gt; results = Project.FlattenToThis(projects);
  
                      return results
  </code></pre>
  
  <h2>                    }</h2>
  
  <p>Sixth post
  namespace CMSEFModel
  {
      using System;
      using System.Collections.Generic;</p>
  
  <pre><code>public partial class GroupModel
  {
      public GroupModel()
      {
          this.ApplicationsGroupsLKs = new HashSet&lt;ApplicationsGroupsLK&gt;();
          this.GroupApplicationConfigurationsLKs = new HashSet&lt;GroupApplicationConfigurationsLK&gt;();
          this.UsersGroupsLKs = new HashSet&lt;UsersGroupsLK&gt;();
      }
  
      public int GroupNumber { get; set; }
      public string GroupName { get; set; }
      public int GroupRank { get; set; }
      public bool ActiveFlag { get; set; }
      public System.DateTime DateAdded { get; set; }
      public string AddedBy { get; set; }
      public System.DateTime LastUpdated { get; set; }
      public string LastUpdatedBy { get; set; }
      // Application - Lazy Loading population
      public int ApplicationID { get; set; }
      // UsersGroupsLK - Lazy Loading population
      public int UserNumber { get; set; }
      public string UserID { get; set; }
  
      public virtual ICollection&lt;ApplicationsGroupsLK&gt; ApplicationsGroupsLKs { get; set; }
      public virtual ICollection&lt;GroupApplicationConfigurationsLK&gt; GroupApplicationConfigurationsLKs { get; set; }
      public virtual ICollection&lt;UsersGroupsLK&gt; UsersGroupsLKs { get; set; }
  
      public GroupModel()
      {}
      public GroupModel(GroupModel group)
       {
          GroupNumber = group.GroupNumber;
          GroupName = group.GroupName;
          ActiveFlag = group.ActiveFlag;
          DateAdded = group.DateAdded;
          AddedBy = group.AddedBy;
          LastUpdated = group.LastUpdated;
          LastUpdatedBy = group.LastUpdatedBy;
          UserNumber = group.UsersGroupsLKs.
       }
  
  
   // Neat Linq trick to convert database query results directly to Model
   public static IList&lt;GroupModel&gt; FlattenToThis(IList&lt;GroupModel&gt; groups)
   {
      return groups.Select(group =&gt; new GroupModel(group)).ToList();
   }
  }
  </code></pre>
  
  <h2>}</h2>
  
  <p>Seventh Post - this is the model I am having trouble with about the errors I previously posted. Trey, if you could help, I WOULD REALLY APPRECIATE IT.... I'm "dead in the water" unless I can get this part working. </p>
  
  <pre><code>namespace CMSEFModel
  {
      using System;
      using System.Collections.Generic;
  
      public partial class Group
      {
          public Group()
          {
              this.ApplicationsGroupsLKs = new HashSet&lt;ApplicationsGroupsLK&gt;();
              this.GroupApplicationConfigurationsLKs = new HashSet&lt;GroupApplicationConfigurationsLK&gt;();
              this.UsersGroupsLKs = new HashSet&lt;UsersGroupsLK&gt;();
          }
  
          public int GroupNumber { get; set; }
          public string GroupName { get; set; }
          public int GroupRank { get; set; }
          public bool ActiveFlag { get; set; }
          public System.DateTime DateAdded { get; set; }
          public string AddedBy { get; set; }
          public System.DateTime LastUpdated { get; set; }
          public string LastUpdatedBy { get; set; }
          // Application - Lazy Loading population
          public int ApplicationID { get; set; }
          // UsersGroupsLK - Lazy Loading population
          public int UserNumber { get; set; }
          public string UserID { get; set; }
  
          public virtual ICollection&lt;ApplicationsGroupsLK&gt; ApplicationsGroupsLKs { get; set; }
          public virtual ICollection&lt;GroupApplicationConfigurationsLK&gt; GroupApplicationConfigurationsLKs { get; set; }
          public virtual ICollection&lt;UsersGroupsLK&gt; UsersGroupsLKs { get; set; }
  
          public GroupModel(Group group)
           {
              GroupNumber = group.GroupNumber;
              GroupName = group.GroupName;
              ActiveFlag = group.ActiveFlag;
              DateAdded = group.DateAdded;
              AddedBy = group.AddedBy;
              LastUpdated = group.LastUpdated;
              LastUpdatedBy = group.LastUpdatedBy;
              UserNumber = group.UsersGroupsLKs.
           }
  
       // Neat Linq trick to convert database query results directly to Model
       public static IList&lt;GroupModel&gt; FlattenToThis(IList&lt;Group&gt; groups)
       {
          return groups.Select(group =&gt; new GroupModel(group)).ToList();
       }
      }
  </code></pre>
  
  <h2>    }</h2>
  
  <p>Eight post:</p>
  
  <pre><code>using (wmswebEntities DbContext = new wmswebEntities())
                  {
                      DbContext.Configuration.ProxyCreationEnabled = false;
              DbContext.Configuration.LazyLoadingEnabled = true;
                      DbContext.Database.Connection.Open();
  
                      List&lt;Groups&gt; myGroups = new List&lt;Groups&gt;();
  
                       var myGroups = from p in DbContext.Groups
                                  where p.ActiveFlag = true
                                  select new
                                  {
                                      p.Groups.ApplicationName,
                                      p.Groups.GroupName,
                                      p.Groups.GroupRank,
                                      p.Groups.ActiveFlag,
                                      p.Groups.DateAdded,
                                      p.Groups.AddedBy,
                                      p.Groups.LastUpdated,
                                      p.Groups.LastUpdatedBy,
                                      p.Groups.ApplicationsGroupsLK.ApplicationID,
                                      p.Groups.UsersGroupsLK.UserNumber
                                  };
  
                      return myGroups;
                  }
  </code></pre>