尝试将 string[] 转换为 Dictionary时出现几个异常错误
我有以下代码,它在换行符上拆分字符串并将其转换为字典以供进一步处理:
string[] splitProgram = program.Split(Environment.NewLine.ToCharArray());
short i = 0;
Dictionary<short, string> programDictionary = splitProgram.ToDictionary<short, string>((value) => i++);
奇怪的是我在第三行出现以下错误:
Error 1 Instance argument: cannot convert from 'string[]' to 'System.Collections.Generic.IEnumerable<short>'
Error 2 'string[]' does not contain a definition for 'ToDictionary' and the best extension method overload 'System.Linq.Enumerable.ToDictionary<TSource,TKey>(System.Collections.Generic.IEnumerable<TSource>, System.Func<TSource,TKey>)' has some invalid arguments
Error 3 Argument '2': cannot convert from 'lambda expression' to 'System.Func<short,string>'
我完全被这个问题难住了,无法弄清楚。有人可以帮忙吗?
I have the following code that splits a string on newlines and converts it to a Dictionary for further processing:
string[] splitProgram = program.Split(Environment.NewLine.ToCharArray());
short i = 0;
Dictionary<short, string> programDictionary = splitProgram.ToDictionary<short, string>((value) => i++);
What's odd is i get the following errors on the third line:
Error 1 Instance argument: cannot convert from 'string[]' to 'System.Collections.Generic.IEnumerable<short>'
Error 2 'string[]' does not contain a definition for 'ToDictionary' and the best extension method overload 'System.Linq.Enumerable.ToDictionary<TSource,TKey>(System.Collections.Generic.IEnumerable<TSource>, System.Func<TSource,TKey>)' has some invalid arguments
Error 3 Argument '2': cannot convert from 'lambda expression' to 'System.Func<short,string>'
I'm absolutely stumped on this and can't figure it out. Can someone help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否注意到
ToDictionary
中 Source 和 Key 的顺序?你可以尝试:
Did you notice the order of Source and Key in
ToDictionary<TSource,TKey>
?You could try :