Linq to Entities例外

时间:2011-08-05 06:31:29

标签: linq linq-to-entities

您好我有这样的查询:

var queryGridData = from question in questions
   select new {
      i = question.Id,
      cell = new List<string>() { question.Id.ToString(), question.Note, question.Topic }
   };

转换ToString()所需的int部分导致:

LINQ to Entities does not recognize the method 'System.String.ToString()' method, and this method cannot be translated into a store expression.

Hmmmmmmmmmmm。我需要string进入collection。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

我个人会在数据库中执行足够的查询以提供您想要的值,并在.NET中完成其余的工作:

var queryGridData = questions.Select(q => new { q.Id, q.Note, q.Topic })
                             .AsEnumerable() // Do the rest locally
                             .Select(q => new { i = q.Id,
                                                cell = new List<string> { 
                                                    q.Id.ToString(),
                                                    q.Note,
                                                    q.Topic
                                                } });

(这种格式很糟糕,但希望在你有更多空间的IDE中做得更好:)