为什么Linq的订单并不总是遵守当前文化?
我遇到一个问题,即在包含字符串的 List
示例:
Thread.CurrentThread.CurrentCulture = new CultureInfo("sv-SE");
var people = new List<object>{"Åke", "Adam", "Bengan"};
return people.OrderBy(p => p);
名称为瑞典语,应排序为:
- Adam
- Bengan
- Åke
此示例非常简化,我需要为列表使用通用数据类型 object
。
该代码在 .NET 4.8 WCF 服务内运行,我尝试以多种不同的方式强制执行这种文化。目前,在我的计算机 (Win 10) 上,WCF 服务返回美国排序:1. Adam 2. Åke 3. Bengan,但是当我编写单元测试时,代码返回正确的瑞典排序。我还有几个外部测试服务器,它们的结果有所不同,一些从 WCF 服务返回正确的瑞典语排序,而另一些则不返回。我需要一种严格执行排序文化的方法。
还有什么会影响排序方式?我已阅读 SortVersion 但无法找到如何将其应用到我的示例中。
干杯!
更新 1
运行时:RuntimeInformation.FrameworkDescription
单元测试和 WCF 服务均返回:.NET Framework 4.8.4420.0
。
Thread.CurrentThread.CurrentCulture = new CultureInfo("sv-SE");
Comparer<object>.Default.Compare("Åke", "Bengan");
单元测试返回1
(这是正确的),WCF 服务返回:-1
。
更新2
关于版本。所有计算机都安装了 .NET Framework 4.8。
带有瑞典字母的 OrderBy 在此版本上工作:
- Windows Server 2016 Standard
但在以下版本上不起作用:
- Windows 10 企业版:21H2
- Windows Server 2019 版本:2004
我们没有其他此时重现此错误的机器。 必须有一种方法可以在所有机器上强制执行文化特定排序?
I have a problem where doing an OrderBy
on a List<object>
containing strings is returning different results in different environments, even though the CurrentCulture is set the same.
Example:
Thread.CurrentThread.CurrentCulture = new CultureInfo("sv-SE");
var people = new List<object>{"Åke", "Adam", "Bengan"};
return people.OrderBy(p => p);
The names are in Swedish and should be ordered:
- Adam
- Bengan
- Åke
This example is very much simplified, and I need to use a generic data type object
for the list.
The code is run inside a .NET 4.8 WCF-service, and I've tried enforcing the culture in many different ways. For now on my machine (Win 10) the WCF service returns US-sort: 1. Adam 2. Åke 3. Bengan, but when I write a unit test, the code returns the correct Swedish-sort. I also have a couple of external test-servers where the result differ, some return the correct Swedish sort from the WCF-service and others not. I need a way of strictly enforcing the culture for sorting.
What more could impact the way of sorting? I have read up on SortVersion but can not find out how to apply it to my example.
Cheers!
UPDATE 1
When running: RuntimeInformation.FrameworkDescription
both the unit test and WCF service returns: .NET Framework 4.8.4420.0
.
Thread.CurrentThread.CurrentCulture = new CultureInfo("sv-SE");
Comparer<object>.Default.Compare("Åke", "Bengan");
The unit test returns 1
(which is correct), the WCF service returns: -1
.
UPDATE 2
Regarding versions. All machines have .NET Framework 4.8 installed.
OrderBy with Swedish letters works on this version:
- Windows Server 2016 Standard
But doesn't work on:
- Windows 10 Enterprise version: 21H2
- Windows Server 2019 version: 2004
We have no other machines to reproduce this bug on at this time.
There must be a way to enforce the culture specific sort on all machines?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论