views:

75

answers:

3

I have written the below

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

Getting error

No overload for method 'Add' takes '1' arguments

Where I am making the mistake

I am using C#3.0

Thanks

A: 

Instead of select new Dictionary<DateTime, double> it should be select new KeyValuePair<DateTime, double>(...) and ToDictionary() should be given two delegates to select key and value.

brickner
A: 

Well, you're calling ToDictionary with no arguments other than the implicit first one. You need to tell it which part of the input sequence is the key, and which is the value. You're also trying to select a new dictionary for each element, which I very much doubt you want to do. Try this:

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

Are you sure you need all those calls to ToList, by the way? It seems somewhat unusual.

Jon Skeet
+1  A: 

Try with:

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

If you are using dictionary you should mention the key and value. Its not like a list.

If it in a list:

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