title: I need to speed up the following Linq query
body: <p>I have an old stored proc I am rewriting into an EF Linq query however the proc is almost 3 times faster!</p>
  
  <p>here is an example of the  the query syntax:</p>
  
  <pre><code>public string GetStringByID(long ID)
      {
          return dataContext.Table2.FirstOrDefault(x =&gt; x.Table2ID == ID).Table1.StringValue;
      }
  </code></pre>
  
  <p>here is the sproc code I am using along with the method of calling it.</p>
  
  <p>sproc is:</p>
  
  <pre><code>PROCEDURE [dbo].[MyQuickerProc]
  @ID bigint
  AS
  BEGIN
  SET NOCOUNT ON;
  
  IF EXISTS(SELECT TOP 1 ID FROM Table2 WHERE Table2ID = @Id)
      BEGIN
          SELECT TOP 1 t1.StringValue
          FROM Table2  t2
              INNER JOIN Table1 t1 ON t1.Table1ID= Table2.Table1ID
          WHERE Table2ID = @ID
      END
  ELSE
      BEGIN
          SELECT TOP 1 t1.StringValue
          FROM Table2 t2
              INNER JOIN Table1 t1 ON t1.Table1Id = Table2.Table1ID
          WHERE Table2ID IS NULL
      END
  
  END
  </code></pre>
  
  <p>I call the proc like this:</p>
  
  <pre><code>string myString = context.MyQuickerProc(127).FirstOrDefault();
  </code></pre>
  
  <p>I have used unit test and stop watch to discover that the Linq call takes 1.3 seconds and the sproc call takes 0.5 seconds, shockingly long! I am investigating missing FK as we speak as I can only assume that is the reason these calls are taking so long.</p>
  
  <p>In any case I need to speed up this Linq query and add the missing functionality that the sproc has and the current Linq query does not contain (the if/else logic).</p>
  
  <p>Any help on this would be much appreciated. thanks in advance :)</p>