Linq 按国家字符集排序

发布于 2024-11-07 11:39:44 字数 662 浏览 6 评论 0原文

我有一个列表,我按字母顺序对某个属性进行排序。

不幸的是,数据库设置(我猜)在获取数据时无法正确处理国家字符。但我无法更改这一点(不是我的数据库),所以我需要直接解决这个问题。因此我简化了下面的例子。

            var x1 = new Shop() {city = "Århus"};
            var x2 = new Shop() { city = "Ans" };
            var x3 = new Shop() { city = "Balle" };

            list.Add(x1);
            list.Add(x2);
            list.Add(x3);
            list = list.OrderBy(a => a.city).ToList();

            //Result:
            // Århus, Ans, Balle
            // Should be:
            // Ans, Balle, Århus

注释应该解释我的问题:“Å”是丹麦语字符集中的最后一个字母

我无法控制应用程序设置,但我可以强制 OrderBy 使用特定字符集进行排序吗?

提前致谢,

斯蒂恩·佩德森

I have a List which i OrderBy alfabetically on a property.

Unfortunatly the database setup (i guess) does not handle national characters properly when fetching the data..I cannot change that though (not my database), so I need to solve the problem directly on this specific. I have therefore simplified the example below.

            var x1 = new Shop() {city = "Århus"};
            var x2 = new Shop() { city = "Ans" };
            var x3 = new Shop() { city = "Balle" };

            list.Add(x1);
            list.Add(x2);
            list.Add(x3);
            list = list.OrderBy(a => a.city).ToList();

            //Result:
            // Århus, Ans, Balle
            // Should be:
            // Ans, Balle, Århus

The commentes should explain my problem: The "Å" is the last letter in danish character set

I am not in control over the application settings, but can I force the OrderBy to order using a specific caracter set?

Thanks in advance,

Steen Pedersen

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小帐篷 2024-11-14 11:39:44

您可以在订购时指定文化。例如:

class Program
{
    static void Main()
    {
        // Set Denmark as current culture
        Thread.CurrentThread.CurrentCulture = new CultureInfo("da-DK");
        var cities = new[] { "Århus", "Ans", "Balle" };

        // When ordering the cities indicate that you want to 
        // take the current culture into account
        cities = cities.OrderBy(a => a, StringComparer.CurrentCulture).ToArray();

        foreach (var city in cities)
        {
            Console.WriteLine(city);
        }
    }
}

打印:

Ans
Balle
Århus

You could specify a culture when ordering. For example:

class Program
{
    static void Main()
    {
        // Set Denmark as current culture
        Thread.CurrentThread.CurrentCulture = new CultureInfo("da-DK");
        var cities = new[] { "Århus", "Ans", "Balle" };

        // When ordering the cities indicate that you want to 
        // take the current culture into account
        cities = cities.OrderBy(a => a, StringComparer.CurrentCulture).ToArray();

        foreach (var city in cities)
        {
            Console.WriteLine(city);
        }
    }
}

prints:

Ans
Balle
Århus
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文