【问题标题】:convert dataReader to Dictionary将 dataReader 转换为 Dictionary
【发布时间】:2012-08-07 09:08:32
【问题描述】:

我尝试使用 LINQ 将一行转换为 Dictionary (fieldName -> fieldValue)

return Enumerable.Range(0, reader.FieldCount)
                 .ToDictionary<string, object>(reader.GetName, reader.GetValue);

但我收到错误消息:

实例参数:无法从'System.Collections.Generic.IEnumerable&lt;int&gt;' 转换为'System.Collections.Generic.IEnumerable&lt;string&gt;'

如何解决这个问题?

【问题讨论】:

标签: linq dictionary datareader


【解决方案1】:
return Enumerable.Range(0, reader.FieldCount)
                 .ToDictionary(
                     i => reader.GetName(i),
                     i => reader.GetValue(i));

【讨论】:

  • 这不是和OP的说法一模一样吗?为什么这不会产生同样的错误?
猜你喜欢
  • 2015-07-20
  • 1970-01-01
  • 2020-07-22
  • 1970-01-01
  • 2020-12-02
  • 1970-01-01
  • 2016-07-01
  • 2015-10-24
  • 1970-01-01
相关资源
最近更新 更多