排序列表导致转换“无效"错误 [英] Sorting List leads to conversion 'void' error

查看:47
本文介绍了排序列表导致转换“无效"错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无法弄清楚为什么我对列表进行排序时出现无效错误.parameterList最初是一本字典,然后将其转换为通用列表,然后尝试排序.如您所见,我想将列表作为通用列表:

Can't figure out why I get a void error with this attempt to sort my list. parameterList is a dictionary originally then I convert it to a generic list then try sorting. I want back the list as a generic list as you can see:

   List<KeyValuePair<string,string>> sortedList = parameterList.ToList().Sort((left, right) => left.Key.Equals(right.Key, StringComparison.Ordinal)
                                                    ? string.Compare(left.Value, right.Value, StringComparison.Ordinal) 
                                                    : string.Compare(left.Key, right.Key, StringComparison.Ordinal));

错误:"无法将源类型'void'转换为目标类型List< System.Collections.Generic.KeyValuePair< string,string>>

这是在说什么空虚....??

What is this void it's talking about....??

已更新

使用我从 http://oauth.googlecode.com/svn/code/csharp/OAuthBase.cs

这就是我的想法:

        Dictionary<string, string> authParamsNonNormalized = new Dictionary<string, string> {
                                                                                                  {Constants.OAuthConsumerKey, consumerKey},
                                                                                                  {Constants.OAuthSignatureMethodKey, methodType},
                                                                                                  {Constants.OAuthTimestampKey, timeStamp()},
                                                                                                  {Constants.OAuthTokenKey, accessToken},
                                                                                                   {Constants.OAuthNonceKey, nonce},
                                                                                                  {Constants.OAuthVersionKey, Constants.OAuthVersion}
                                                                                              };

然后我使用ToList()t将其转换为通用列表,因为这是我的方法的传入字典,其中包含以下代码:

Then I convert it to a Generic list with the ToList()t as this was an incoming dictionary to my method that contains this code:

List<KeyValuePair<string,string>> sortedParamList = parameterList.ToList();
            sortedParamList.OrderBy(p => p.Key, StringComparer.Ordinal)
                                    .ThenBy(p => p.Value, StringComparer.Ordinal).ToList();

当我检查sortedParamList时,它仍然是相同的顺序……什么都没发生.

when I check sortedParamList, it's still in the same order...nothing happened.

已更新:

废话,是的,我搞砸了最后一个,这是工作代码:

crap, yea I screwed up the last one, here is the working code:

List<KeyValuePair<string, string>> sortedParamList = parameterList.OrderBy(p => p.Key, StringComparer.Ordinal)
                            .ThenBy(p => p.Value, StringComparer.Ordinal).ToList();

推荐答案

<代码> List< T> .Sort 不返回已排序的列表;相反,它执行就地"排序,从而更改了调用它的列表实例.您可能打算使用:

List<T>.Sort does not returned the sorted list; rather, it performs the sort "in-place", altering the list instance on which it is called. You probably mean to use:

List<KeyValuePair<string, string>> sortedList = parameterList.ToList();
sortedList.Sort((left, right) => left.Key.Equals(right.Key, StringComparison.Ordinal)
    ? string.Compare(left.Value, right.Value, StringComparison.Ordinal)
    : string.Compare(left.Key, right.Key, StringComparison.Ordinal));

以下LINQ在语义上是等效的,但更加清晰:

The following LINQ is semantically equivalent, but significantly clearer:

List<KeyValuePair<string, string>> sortedList =
    parameterList.OrderBy(p => p.Key, StringComparer.Ordinal)
                 .ThenBy(p => p.Value, StringComparer.Ordinal)
                 .ToList();

这篇关于排序列表导致转换“无效"错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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