2012-12-11 95 views
2

我试图做一个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. 

,这是什么解决办法吗?

回答

3

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(); 
+0

那么我已经写了这个,但我正在寻找更简洁的东西。 – TheVillageIdiot