B. *_*non 2 c# sorting tstringlist
我想按长度降序对短语的字符串列表进行排序,以便:
Rory Gallagher
Rod D'Ath
Gerry McAvoy
Lou Martin
Run Code Online (Sandbox Code Playgroud)
最终会是:
Rory Gallagher
Gerry McAvoy
Lou Martin
Rod D'Ath
Run Code Online (Sandbox Code Playgroud)
我想先试试这个:
List<string> slPhrasesFoundInBothDocs;
. . . // populate slPhrasesFoundInBothDocs
slPhrasesFoundInBothDocs = slPhrasesFoundInBothDocs.OrderByDescending(x => x.Length);
Run Code Online (Sandbox Code Playgroud)
...但最后一行无法编译,intellisense 建议我将其更改为:
slPhrasesFoundInBothDocs = (List<string>)slPhrasesFoundInBothDocs.OrderByDescending(x => x.Length);
Run Code Online (Sandbox Code Playgroud)
...我做到了。它可以编译,但会引发运行时异常,即“无法转换类型为 'System.Linq.OrderedEnumerable 2[System.String,System.Int32]' to type 'System.Collections.Generic.List1[System.String]' 的对象。 ”
我需要修复此代码,还是以完全不同的方式对其进行攻击?
用这个:
slPhrasesFoundInBothDocs =
slPhrasesFoundInBothDocs
.OrderByDescending(x => x.Length)
.ToList();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
126 次 |
| 最近记录: |