Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linq 使用IsNullOrWhitespace检查从IQueryable中选择值_Linq_Linq To Entities_Iqueryable - Fatal编程技术网

Linq 使用IsNullOrWhitespace检查从IQueryable中选择值

Linq 使用IsNullOrWhitespace检查从IQueryable中选择值,linq,linq-to-entities,iqueryable,Linq,Linq To Entities,Iqueryable,我正在尝试使用IQueryable表达式执行以下操作: (from Person p in s select new { label = p.FirstName + " " + (string.IsNullOrWhiteSpace(p.MiddleName) ? p.MiddleName + " " : "") + p.LastName, value = p.Id }).ToList

我正在尝试使用
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.

解决方案是什么?

String.IsNullOrWhitespace是String对象的静态函数,不能用于实体框架查询,而
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();

我已经写了这篇文章,但我在寻找更简洁的东西。