我实现了扩展方法标准化在这个帖子中描述的字符串:LINQ Where Ignore Accentuation and Case使用C#扩展方法在LINQ查询谓词
这种方法的工作一样,如果我做这样的事情魅力:
employee.AsQueryable().Where(t=>t.Text.ToLower().RemoveDiacritics().Contains("ced"));
现在,我想通过动态地生成where子句的谓词来更一般地使用它。
var values = filters.Select(f => f.Value is string && f.IgnoreAccent
?((string)f.Value).RemoveDiacritics()
:f.Value).ToArray();
// Create a predicate expression
string predicate = filter.ToExpression(filters);
// Use the Where method of Dynamic Linq to filter the data
queryable = queryable.Where(predicate, values);
谓词看起来就像这样:
(Text.ToLower().RemoveDiacritics().Contains(@0))
对于一个未知的原因,在执行时,我得到了以下错误消息:
没有适用的方法“RemoveDiacritics”存在型'String'
但是,这种方法实际上工作正常我在别处使用它。
任何想法这里有什么错?
请注意,在这种情况下,ToLower()
就像一个魅力。
在此先感谢您的帮助!
EDIT
这里是扩展方法的定义:
public static class StringExtension
{
public static string RemoveDiacritics(this String s)
{
String normalizedString = s.Normalize(NormalizationForm.FormD);
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < normalizedString.Length; i++)
{
Char c = normalizedString[i];
if (CharUnicodeInfo.GetUnicodeCategory(c) != UnicodeCategory.NonSpacingMark)
stringBuilder.Append(c);
}
return stringBuilder.ToString();
}
}
您是否在依赖该方法的文件中包含了方法扩展的名称空间? –
另外,你使用命名空间别名? –
在第一种情况下,您仍然有一个对象t,而在第二种情况下,您将f赋给一个字符串。 – jdweng