如何使用Linq将float []的索引/值与Dictionary <string,int_to_merge =“”> </string,>合并

时间:2011-01-17 23:17:54

标签: c# arrays linq dictionary

假设我有float[],其中数组中的每个位置都具有唯一含义。每个位置对应于以下字典中的条目,其中int指的是数组偏移:

// The int below refers to a position within the float
Dictionary<myObjectDetail, int> 

我想加入float[index_to_merge]Dictionary<myObjectDetail,index_to_merge>,所以我最终会得到类似

的内容
  Dictionary<myObjectDetail,floatvalue>

我可以使用Linq解决此问题,并将这些对象合并为一个新的匿名类型吗?

1 个答案:

答案 0 :(得分:2)

您可以使用以下方法实现这一目标:

var newDictionary = oldDictionary.ToDictionary(x => x.Key, x => floats[x.Value]);

此处oldDictionary是您的原始Dictionary<myObjectDetail, int>,现在您的newDictionaryDictionary<myObjectDetail,floatvalue>,这就是您想要的。

相关问题