我有以下代码:
我有以下代码:
var countries = from c in db.Countries
where (string.IsNullOrWhiteSpace(searchAlpha2) || (c.Alpha2 ?? string.Empty).ToUpper().Contains(searchAlpha2.ToUpper()))
&& (string.IsNullOrWhiteSpace(searchAlpha2) || (c.Alpha3 ?? string.Empty).ToUpper().Contains(searchAlpha3.ToUpper()))
&& (string.IsNullOrWhiteSpace(searchName) || (c.Name ?? string.Empty).ToUpper().Contains(searchName.ToUpper()))
select c;
这段代码使用Entity Framework v6 Code First与SQL数据库交互。
除了性能方面的考虑,如果我不包含IsNullOrWhitespace
,当过滤条件为空时(我测试过null和空值),我将得不到任何结果;然而当有值存在时,它可以按预期工作。
我遇到了以下错误:
LINQ to Entities does not recognize the method 'Boolean IsNullOrWhiteSpace(System.String)' method, and this method cannot be translated into a store expression.
我正在尝试使用searchXXX字符串来过滤列。我尝试使用RegEx.IsMatch、SqlMethods.Like以及下面的代码,但都会出现错误,说这些函数不被允许(错误来自于EntityFramework.SqlServer
或Linq to Entities
)。我在这里看到了很多成功实现的帖子,所以想知道是否有基本的东西我没有注意到?
IsNullOrWhiteSpace
。 - Matas Vaitkevicius