Linq OrderBy在泛型列表上返回的不完全是字母列表 [英] Linq OrderBy on generic list returns not entirely alphabetical list

查看:129
本文介绍了Linq OrderBy在泛型列表上返回的不完全是字母列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用对象的Name属性对对象的一般列表进行排序.我正在使用LINQ,以下表达式不太起作用:

I am trying to sorting a generic list of objects using the objects Name property. I am using LINQ and the following expressions doesn't quite work:

var query = possibleWords.OrderBy(x => x.Name.ToLower()).ToList();
foreach (Word word in query) //possibleWords.OrderBy(word => word.Name))
   {
            listWords.Items.Add(word.Name);
   }

查询"现在应该包含一个已订购商品的列表,如果我正确理解的话,则应该将商品添加到名为listWords的列表框中.

"query" should now contain a list of ordered items, if I understand it correctly and item should be added to the listbox named listWords.

但是输出是这样的:

http://screencast.com/t/s1CkkWfXD4 (很抱歉,URL链接,但是以某种方式将我拒之门外的帐户,显然我无法使用此新帐户发布图片.

http://screencast.com/t/s1CkkWfXD4 (sorry for the URL link, but SO has somehow locked me out of my account and I apparently can't post images with this new one).

该列表框几乎是字母顺序的,但并不完全是字母顺序的.由于某种原因,"aa"和"aaaa"排在最后.可能是什么原因,以及如何解决?

The listbox is almost alphabetical but not quite. For some reason "aa" and "aaaa" comes last. What can be the reason, and how to resolve it?

谢谢.

根据请求进行详细说明

此代码在Visual Studio中输入并执行时:

This code, when entered in Visual Studio and executed:

        List<Word> words = new List<Word>();

        words.Add(new Word("a"));
        words.Add(new Word("Calculator"));
        words.Add(new Word("aaa"));
        words.Add(new Word("Projects"));
        words.Add(new Word("aa"));
        words.Add(new Word("bb"));
        words.Add(new Word("c"));

        IEnumerable<Word> query = words.OrderBy(x => x.Name.ToLower()).ToList();

        foreach (Word word in query)
        {
            Console.WriteLine(word.Name);
        }

给我以下输出:

a
bb
c
Calculator
ccc
Projects
aa
aaa

此排序方式不正确:第一个"a"是正确的,但随后的"aa"和"aaa"条目被发送到列表的底部.

This is not sorted correctly: The first "a" is correct, but the subsequent "aa" and "aaa" entries are sent to the bottom of the list.

我对字符集和编码不太了解,因此我可能在这里犯了一个菜鸟错误.但是在那种情况下,我不知道这可能是什么,而为什么第一个"a"正确排序却使第二个和第三个"aa"以及"aaa"不正确令我有些困惑!

I'm not too knowledgeable about character sets and encoding, so possibly I am making a rookie mistake here. But in that case I do not recognize what that might be, and I would be a bit puzzled as to why the first "a" is ordering correctly, but the second and third "aa" and "aaa" is not!

进一步阐述-Word类

[Serializable()]
public class Word
{
    [System.Xml.Serialization.XmlAttribute("Name")]
    public string Name { get; set; }

    public Word(string name)
    {
        Name = name;
    }

    public Word() { } //Parameter less constructor neccessary for serialization

}

原因和解决方法

就像@道格拉斯建议的那样,通过为OrderBy方法提供StringComparer.InvariantCultureIgnoreCase比较器可以解决此问题.

Like @Douglas suggested, the problem was resolved by supplying the StringComparer.InvariantCultureIgnoreCase comparer to the OrderBy method.

在进一步的研究中,当使用丹麦文化(da-DK)时,似乎FindAll和OrderBy方法(可能还有其他方法)都存在问题.可能还有其他方法或文化失败,但是da-DK文化和FindAll + OrderBy方法肯定无法按预期工作.

On further research, it seems both the FindAll and OrderBy methods (possibly others) have problems, when using Danish culture (da-DK). There might be other methods or cultures that fail, but da-DK culture and FindAll + OrderBy methods definitely are not working as intended.

OrderBy方法存在此线程中描述的问题(错误的排序). FindAll方法有一个类似的非常奇怪的问题:假设我们有一个条目列表:a,aa,aaa和aaaa.当使用FindAll(x => x.StartsWith("a"))时,它将仅返回"a"而不是aa,aaa和aaaa.如果使用StartsWith("aa"),它将正确找到aa以及aaa和aaaa.当使用StartWith("aaa")时,它将再次找不到aaaaa,只有aaa!这似乎是框架中的错误.

The OrderBy method has the problem as described in this thread (wrongful ordering). The FindAll method has a similar, very strange problem: Assume we have a list of entries: a, aa, aaa and aaaa. When using FindAll(x => x.StartsWith("a")), it will only return "a" NOT aa, aaa and aaaa. If using StartsWith("aa"), it will correctly find aa, as well as aaa and aaaa. When using StartWith("aaa") it will again not find aaaa, only aaa! This seems to be a bug in the framework.

推荐答案

您可以尝试替换:

IEnumerable<Word> query = words.OrderBy(x => x.Name.ToLower()).ToList();

…与:

IEnumerable<Word> query = words.OrderBy(x => x.Name, 
    StringComparer.InvariantCultureIgnoreCase);

这是一个很奇怪的文化问题.

There's a very small chance that it's a weird culture issue.

这篇关于Linq OrderBy在泛型列表上返回的不完全是字母列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆