3

我正在尝试使用IQueryable表达式执行以下操作:

(from Person p in s
   select new
   {
           label = p.FirstName + " "
       + (string.IsNullOrWhiteSpace(p.MiddleName) ? p.MiddleName + " " : "")
                   + p.LastName,
       value = p.Id
       }).ToList();

我收到以下错误:

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

解决方案是什么?

4

1 回答 1

4

String.IsNullOrWhitespace 是字符串对象的静态函数,不能用于实体框架查询,而是p.FirstName.StartsWith("S")实体属性的方法,可以使用。

要回答您的问题,您将不得不滚动自己的内联。尝试这个:

(from Person p in s
   select new
   {
       label = p.FirstName + " "
       + ((p.MiddleName != null && p.MiddleName != string.Empty) ? p.MiddleName + " " : "")
                   + p.LastName,
       value = p.Id
   }).ToList();
于 2012-12-11T03:25:54.577 回答