title: How do I optimize this SharePoint query for Terms?
body: <p>I have a Managed Metadata Service which has a Term Group with a Term Set and Terms.</p>
  
  <p>I am new to SharePoint querying and I am currently doing the following:</p>
  
  <ol>
  <li>Get client context</li>
  <li>Set credentials</li>
  <li>Update client context cache</li>
  <li>Get taxonomy session</li>
  <li>Get term store</li>
  <li>Get term set</li>
  <li>Get terms</li>
  </ol>
  
  <p>For every one of the above steps, I am loading and executing a query on the client context.</p>
  
  <ol>
  <li>Is there a better way to load and execute queries?</li>
  <li>Is there a direct LINQ query or CAML query that I can use to get the terms I need given the UID of the term set?</li>
  </ol>
  
  <p><strong>Code:</strong></p>
  
  <pre><code>var siteUrl = ConfigHelper.GetValue("SharepointSiteUrl");
  var clientContext = new ClientContext(siteUrl);
  
  clientContext.Credentials = new NetworkCredential(ConfigHelper.GetValue("ServiceAccountLogonName"), ConfigHelper.GetValue("ServiceAccountPassword"));
  
  var taxonomySession = TaxonomySession.GetTaxonomySession(clientContext);
  
  taxonomySession.UpdateCache();
  
  clientContext.Load(taxonomySession, ts =&gt; ts.TermStores);
  clientContext.ExecuteQuery();
  
  if (taxonomySession.TermStores.Count == 0)
  {
      throw new InvalidOperationException("The Taxonomy Service is offline or missing");
  }
  
  var termStore = taxonomySession.TermStores[1];
  
  clientContext.Load(termStore);
  clientContext.ExecuteQuery();
  
  var termSet = termStore.GetTermSet(new Guid("f40eeb54-7c87-409d-96c7-75ceed6bff60"));
  clientContext.Load(termSet);
  clientContext.ExecuteQuery();
  
  var terms = termSet.GetAllTerms();
  clientContext.Load(terms);
  clientContext.ExecuteQuery();
  
  foreach (var term in terms)
  {
      clientContext.Load(term, t =&gt; t.Id, t =&gt; t.Name);
      clientContext.ExecuteQuery();                
  }
  </code></pre>
  
  <p>How do I optimize this SharePoint query for Terms?</p>