title: How to use Entity FrameWork ToLookUp()
body: <p>My current query for my grid's ItemSource is dramatically slow. It current has 8 includes. I've read that it can be sped up if I use multiple queries instead of using <code>Includes()</code>. Referencing from <a href="http://mikee.se/Archive.aspx/Details/entity_framework_pitfalls,_include_20140101" rel="nofollow">http://mikee.se/Archive.aspx/Details/entity_framework_pitfalls,_include_20140101</a></p>
  
  <p>I'm written the queries to my database below </p>
  
  <pre><code>var loans = unitOfWork.Context.Loans.AsNoTracking().ToList();
  var clientAccount = unitOfWork.Context.ClientAccounts.AsNoTracking().ToList();
  var business = unitOfWork.Context.Businesses.AsNoTracking().ToList();
  </code></pre>
  
  <p>I'm trying to write the next the ToLookUp part but I'm not quite sure what the proper syntax/way is to do it. </p>
  
  <p>Loan.cs Model</p>
  
  <pre><code>public int? Client_Id { get; set; }
  [ForeignKey("Client_Id")]
  public virtual Business Client { get; set; }
  
  public int? ClientAccount_Id { get; set; }
  [ForeignKey("ClientAccount_Id")]
  public virtual ClientAccount ClientAccount { get; set; }
  </code></pre>
  
  <p>Business.cs Model</p>
  
  <pre><code> public virtual ICollection&lt;Loan&gt; Loans { get; set; }
  </code></pre>
  
  <p>I was able to implement a lookup code if it has a 1 to 1 relationship. For example, a loan can only have 1 client account.</p>
  
  <pre><code>var clientAccount = unitOfWork.Context.ClientAccounts.AsNoTracking().ToLookup(x =&gt; x.LoanId)
  </code></pre>
  
  <p>But if it's not exactly 1 to 1 I'm not sure how I would implement the Lookup and Contains. A business can have multiple loans.</p>
  
  <pre><code>var business = unitOfWork.Context.Businesses.AsNoTracking().ToLookup(x =&gt; x.Loans);
  </code></pre>
  
  <p>Now how do I set the contains? I tried below but that didn't work.</p>
  
  <pre><code>loan.Client = business.Contains(loan.Id) 
  </code></pre>