我正在尝试从中选择Dictionary<String, Double>一个Dictionary<String, String>。为此,我使用IEnumrable<>.Whereso子选择a Dictionary<String, String>(有效),然后将其强制转换为Dictionary<String, Double>(无效)。
我的代码:
//linesTable is an System.data.DataTable object
//...for loop code in here
DataRow row = linesTable.Rows[i]; //Where i is the loop index
Dictionary<String, String> valuesDictionary
= row.Table.Columns.Cast<DataColumn>().ToDictionary(
col => col.ColumnName,
col => row.Field<String>(col.ColumnName));
//ATTEMPT #1
/*Dictionary<String, Double> numericValues = valuesDictionary.Where(
subs =>
{
double doubleValue;
return double.TryParse(subs.Value, out doubleValue);
}).Cast<Dictionary<String, Double>>();*/
//ATTEMPT #2
Dictionary<String, Double> numericValues = valuesDictionary.Where(
subs => {
double doubleValue;
return double.TryParse(subs.Value, out doubleValue);
}
).ToDictionary<String, Double>(
pair => pair.Key,
pair => double.Parse(pair.Value));
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
我之所以需要某种Dictionary<String, Double>格式,是因为我有一个需要将字典数据传递给该类型的方法。
提前致谢!
相似的问题:我的问题与以下问题相似:http : //bit.ly/12qjrmE,http : //bit.ly/YmuRHZ和 http://bit.ly/XLQNaB,除了我希望子查询返回一个Dictionary之外键入。
如果你不需要Dictionary<String, String>,你可以立即制作:
Dictionary<String, Double> valuesDictionary =
row.Table.Columns.Cast<DataColumn>()
.Where(col =>
{
double d;
return double.TryParse(row.Field<String>(col.ColumnName), out d);
})
.ToDictionary(
col => col.ColumnName,
col => double.Parse(row.Field<String>(col.ColumnName)));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
893 次 |
| 最近记录: |