次のコードがあります。
public QuestionDetail GetQuestionDetail(int questionId)
{
Question question = _questionsRepository.GetById(questionId);
QuestionDetail questionDetail = new QuestionDetail()
{
QuestionId = questionId,
Text = question.Text.FormatCode()
};
return questionDetail;
}
これを次のものに置き換えました:
public QuestionDetail GetQuestionDetail(int questionId)
{
var questions = _questionsRepository
.GetAll()
.Include(q => q.Answers)
.Select(m => new QuestionDetail
{
QuestionId = m.QuestionId,
Text = m.Text.FormatCode()
})
.FirstOrDefault();
return questions;
}
次のエラーメッセージが表示されます。
LINQ to Entities does not recognize the method 'System.String FormatCode(System.String)'
method, and this method cannot be translated into a store expression.
ここに私の FormatCode() があります
public static class CodeDisplay {
public static string FormatCode(this string content)
{
var data1 = content
.Split(new[] { "<pre>", "</pre>" }, StringSplitOptions.None);
var data2 = data1
.Select((s, index) =>
{
string s1 = index % 2 == 1 ? string.Format("{0}{2}{1}",
"<table class='code'>", "</table>", SplitJoin(s)) : s;
return s1;
});
var data3 = data2.Where(s => !string.IsNullOrEmpty(s));
var data4 = string.Join("\n", data3);
return data4;
}
private static string SplitJoin(string content)
{
IEnumerable<String> code =
content.Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries)
.Select((line, index) =>
string.Format("<tr><td>{0}</td><td><pre><code>{1}</code></pre></td></tr>\n",
(index + 1).ToString("D2"), HttpUtility.HtmlEncode(line)));
return string.Join("", code) + "\n";
}
}