title: Iterating through datacontext - most efficient way?
body: <p>I have started using performance wizard in visual studio 2012 because there was a slow method which is basically used to get all users from the datacontext. I fixed the initial problem but I am now curious if I can make it faster.</p>
  
  <p>Currently I am doing this:</p>
  
  <pre><code>public void GetUsers(UserManagerDashboard UserManagerDashboard)
  {
      try
      {
          using (GenesisOnlineEnties = new GenesisOnlineEntities())
          {
              var q = from u in GenesisOnlineEnties.vw_UserManager_Users
                      select u;
  
              foreach (var user in q)
              {
                  User u = new User();
                  u.UserID = user.UserId;
                  u.ApplicationID = user.ApplicationId;
                  u.UserName = user.UserName;
                  u.Salutation = user.Salutation;
                  u.EmailAddress = user.Email;
                  u.Password = user.Password;
                  u.FirstName = user.FirstName;
                  u.Surname = user.LastName;
                  u.CompanyID = user.CompanyId;
                  u.CompanyName = user.CompanyName;
                  u.GroupID = user.GroupId;
                  u.GroupName = user.GroupName;
                  u.IsActive = user.IsActive;
                  u.RowType = user.UserType;
                  u.MaximumConcurrentUsers = user.MaxConcurrentUsers;
                  u.ModuleID = user.ModuleId;
                  u.ModuleName = user.ModuleName;
  
                  UserManagerDashboard.GridUsers.users.Add(u);
              }
          }
      }
      catch (Exception ex)
      {
      }
  }
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/PeQka.png" alt="Performance report from VS2012"></p>
  
  <p>It's a very straight forward method. Connect to the database using entity framework, get all users from the view "vw_usermanager_users" and populate the object which is part of a collection.</p>
  
  <p>I was casting ?int to int and I changed the property to ?int so no cast is needed. I know that it is going to take longer because I am looping through records. But is it possible to speed this query up?</p>