title: Speed up EF5 Code First query
body: <p>Following on from previous questions using EF5 Code First, SQL Server 2008 R2 db, Generic Repository and Unit Of Work patterns</p>
  
  <p>I have the following function </p>
  
  <pre><code>public static void MoveGraphicPosition(int queueId, int graphicId, bool moveUp)
  {
      using (var unitOfWork = new GraphicUnitOfWork(ConnGraphics, false))
      {
          var sourceGraphic = unitOfWork.GraphicRepository.FindSingle(g =&gt; g.Id == graphicId);
  
          if (sourceGraphic == null) return;
  
          var startPosition = sourceGraphic.QueuePosition;
  
          Graphic targetGraphic;
  
          if (moveUp)
          {
              targetGraphic =
                  unitOfWork.PlayoutQueueRepository.FindSingle(q =&gt; q.Id == queueId, q =&gt; q.Graphics)
                            .Graphics.Where(g =&gt; g.QueuePosition &lt; startPosition)
                            .OrderByDescending(g =&gt; g.QueuePosition)
                            .Take(1).FirstOrDefault();
          }
          else
          {
              targetGraphic =
                  unitOfWork.PlayoutQueueRepository.FindSingle(q=&gt; q.Id == queueId, q =&gt; q.Graphics)
                            .Graphics.Where(g =&gt; g.QueuePosition &gt; startPosition)
                            .OrderBy(g =&gt; g.QueuePosition)
                            .Take(1).FirstOrDefault();
          }
  
          // Swop the positions
          if (targetGraphic == null) return;
  
          sourceGraphic.QueuePosition = targetGraphic.QueuePosition;
  
          targetGraphic.QueuePosition = startPosition;
  
          unitOfWork.GraphicRepository.Update(sourceGraphic);
          unitOfWork.GraphicRepository.Update(targetGraphic);
  
          // Save to database
          unitOfWork.Save();
      }
  } 
  </code></pre>
  
  <p>Running this method via a Web Api call it takes about 2 seconds to run, I am bit puzzled as to why it takes this long, was expecting less than a second, is there any advice as to speed this up.</p>
  
  <p>All we are trying to do is change queue positions of two graphic objects - swop queue positions around - our current one with the next one in the queue based on position.</p>
  
  <p>Not sure whether this is EF5 or my LINQ query being inefficient.</p>
  
  <p>FindSingle on the repository looks like this</p>
  
  <pre><code>public T FindSingle(Expression&lt;Func&lt;T, bool&gt;&gt; predicate = null, params Expression&lt;Func&lt;T, object&gt;&gt;[] includes)
  {
      var set = FindIncluding(includes);
      return (predicate == null) ? set.FirstOrDefault() : set.FirstOrDefault(predicate);
  } 
  
  public IQueryable&lt;T&gt; FindIncluding(params Expression&lt;Func&lt;T, object&gt;&gt;[] includeProperties)
  {
      IQueryable&lt;T&gt; set = _context.GetEntitySet&lt;T&gt;();
  
      if (includeProperties != null)
      {
          foreach (var include in includeProperties)
          {
              set = set.Include(include);
          }
      }
  
      return set.AsQueryable();
  }
  </code></pre>
  
  <p>I have a feeling might have to change this to take two objects in and then just do the update but this may be masking a problem which we will have to address at a later date as this should be straightforward.</p>