C# LINQ 按错误排序
嘿, 我认为 order by 是错误地排序。示例代码:
static void Main()
{
// Create a delicious data source.
string[] fruits = { "äax", "ääü", "äbü" };
// Query for ascending sort.
IEnumerable<string> sortAscendingQuery =
from fruit in fruits
orderby fruit //"ascending" is default
select fruit;
// Execute the query.
Console.WriteLine("Ascending:");
foreach (string s in sortAscendingQuery)
{
Console.WriteLine(s);
}
}
结果:
Ascending:
ääü
äax
äbü
正确的顺序应该是: 亚克斯 阿布 ääü
有人遇到过这个错误吗?
Hei,
i think order by is ordering things wrongly. Example code:
static void Main()
{
// Create a delicious data source.
string[] fruits = { "äax", "ääü", "äbü" };
// Query for ascending sort.
IEnumerable<string> sortAscendingQuery =
from fruit in fruits
orderby fruit //"ascending" is default
select fruit;
// Execute the query.
Console.WriteLine("Ascending:");
foreach (string s in sortAscendingQuery)
{
Console.WriteLine(s);
}
}
And the result:
Ascending:
ääü
äax
äbü
The right order should be:
äax
äbu
ääü
Anybody encountered this error before?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这将使用
IComparer
的默认实现。这是一种文化敏感的比较,我相信这就是为什么它认为“ääü”位于“äax”之前。您期待顺序比较吗?如果是这样,您可以显式指定比较器,但不能在查询表达式中指定:
如果这不是您要查找的内容,请解释您需要哪种比较。
That's going to use the default implementation of
IComparer<string>
. That's a culture-sensitive comparison, which I believe is why it's deeming "ääü" as coming before "äax".Were you expecting an ordinal comparison? If so, you can specify the comparer explicitly, but not in a query expression:
If that's not what you're looking for, please explain what sort of comparison you need.
此 linq 返回正确的结果 - 您需要指定要使用的字符串比较。
This linq returns the correct results - you need specify the string comparison to use.