【发布时间】:2016-05-05 19:53:57
【问题描述】:
我想要一个像OrderBy() 这样的方法,它总是命令忽略重音字母,并像无重音一样看待它们。我已经尝试覆盖OrderBy(),但似乎我不能这样做,因为这是一个静态方法。
所以现在我想为OrderBy() 创建一个自定义 lambda 表达式,如下所示:
public static IOrderedEnumerable<TSource> ToOrderBy<TSource, TKey>(
this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
{
if(source == null)
return null;
var seenKeys = new HashSet<TKey>();
var culture = new CultureInfo("pt-PT");
return source.OrderBy(element => seenKeys.Add(keySelector(element)),
StringComparer.Create(culture, false));
}
但是,我收到了这个错误:
错误 2 方法的类型参数 'System.Linq.Enumerable.OrderBy
(System.Collections.Generic.IEnumerable , System.Func , System.Collections.Generic.IComparer )' 不能从 用法。尝试明确指定类型参数。
似乎不喜欢StringComparer。我该如何解决这个问题?
注意:
我已经尝试使用here 中的RemoveDiacritics(),但我不知道在这种情况下如何使用该方法。所以我尝试做类似this 之类的东西,看起来也不错。
【问题讨论】:
-
你使用的是 Linq2Sql 还是 LinqObjects ?
-
HashSet 是干什么用的?
标签: c# sorting lambda expression letters