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();
我已经写了这篇文章,但我在寻找更简洁的东西。