title: Can I use an extension method inside a LINQ query?
body: <p>I have the following code:</p>
  
  <pre><code>    public QuestionDetail GetQuestionDetail(int questionId)
      {
          Question question = _questionsRepository.GetById(questionId);
          QuestionDetail questionDetail = new QuestionDetail()
          {
              QuestionId = questionId,
              Text = question.Text.FormatCode()
          };
          return questionDetail;
      }
  </code></pre>
  
  <p>I replaced this with:</p>
  
  <pre><code>    public QuestionDetail GetQuestionDetail(int questionId)
      {
          var questions = _questionsRepository
              .GetAll()
              .Include(q =&gt; q.Answers)
              .Select(m =&gt; new QuestionDetail
              {
                  QuestionId = m.QuestionId,
                  Text = m.Text.FormatCode()
              })
              .FirstOrDefault();
  
          return questions;
      }
  </code></pre>
  
  <p><strong>Now I get the following error message:</strong></p>
  
  <pre><code>LINQ to Entities does not recognize the method 'System.String FormatCode(System.String)' 
  method, and this method cannot be translated into a store expression.
  </code></pre>
  
  <p>Here's my FormatCode()</p>
  
  <pre><code>public static class CodeDisplay {
  
      public static string FormatCode(this string content)
      {
          var data1 = content
              .Split(new[] { "&lt;pre&gt;", "&lt;/pre&gt;" }, StringSplitOptions.None);
          var data2 = data1
              .Select((s, index) =&gt;
              {
                  string s1 = index % 2 == 1 ? string.Format("{0}{2}{1}",
                      "&lt;table class='code'&gt;", "&lt;/table&gt;", SplitJoin(s)) : s;
                  return s1;
              });
          var data3 = data2.Where(s =&gt; !string.IsNullOrEmpty(s));
          var data4 = string.Join("\n", data3);
          return data4;
      }
  
      private static string SplitJoin(string content)
      {
          IEnumerable&lt;String&gt; code =
              content.Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries)
                  .Select((line, index) =&gt;
                      string.Format("&lt;tr&gt;&lt;td&gt;{0}&lt;/td&gt;&lt;td&gt;&lt;pre&gt;&lt;code&gt;{1}&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;\n",
                      (index + 1).ToString("D2"), HttpUtility.HtmlEncode(line)));
          return string.Join("", code) + "\n";
      }
  
  
  }
  </code></pre>