转换为字典的问题<;日期时间,双精度>;()使用LINQ(C#3.0)

转换为字典的问题<;日期时间,双精度>;()使用LINQ(C#3.0),linq,c#-3.0,dictionary,Linq,C# 3.0,Dictionary,我已经写了以下内容 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 } ).

我已经写了以下内容

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>();
return(来自returnObject.portfolions.ToList()中的p)
来自p.childData.ToList()中的childData
来自p.Returns.ToList()中的retuns
选择新字典()
{p.EndDate,retuns.Value}
).ToDictionary();
获取错误

方法“Add”没有重载接受“1”个参数

我在哪里犯了错误

我正在使用C#3.0


感谢而不是
选择新字典
它应该是
选择新的键值对(…)
ToDictionary()
应该有两个代表来选择键值和值。

而不是
选择新字典
它应该是
选择新的键值对(…)
ToDictionary()
应该有两个代理来选择键和值。

好吧,您正在调用
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吗?这似乎有点不寻常。

好吧,您正在调用
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吗?这似乎有点不寻常。

试试:

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();
尝试:

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();