2010-05-20 63 views
0
转换ToDictionary <日期时间,双>()

我已经写了下面问题在使用LINQ(C#3.0)

return (from p in returnObject.Portfolios.ToList() 
     from childData in p.ChildData.ToList() 
     from retuns in p.Returns.ToList() 
     select new Dictionary<DateTime, double>() 
     { p.EndDate, retuns.Value } 
).ToDictionary<DateTime,double>(); 

获取错误

否过载对方法 '添加' 取“1 “论据

当我作出错误

我使用C#3.0

感谢

回答

2

那么,你打电话ToDictionary除了隐式的第一个之外没有其他参数。你需要告诉它输入序列的哪一部分是关键字,哪个是值。你还试图为每个元素选择一个新字典,我非常怀疑你想要做什么。试试这个:

var dictionary = (from p in returnObject.Portfolios.ToList() 
        from childData in p.ChildData.ToList() 
        from returns in p.Returns.ToList() 
        select new { p.EndDate, returns.Value }) 
       .ToDictionary(x => x.EndDate, x => x.Value); 

你确定你需要所有这些调用ToList,顺便说一句吗?这似乎有点不寻常。

0

相反的select new Dictionary<DateTime, double>应该select new KeyValuePair<DateTime, double>(...)ToDictionary()应给予两名代表,选择键和值。

1

尝试:

return (from p in returnObject.Portfolios 
          from childData in p.ChildData 
          from retuns in p.Returns 
          select new 
          {p.EndDate, retuns.Value }).ToDictionary(d => d.EndDate , d=>      
          d.Value); 

如果您使用的词典中,你应该提到的键和值。它不像一个列表。

如果它在一个列表:

return (from p in returnObject.Portfolios 
          from childData in p.ChildData 
          from retuns in p.Returns 
          select new 
          {p.EndDate, retuns.Value }).ToList();