Home > Blockchain >  Get values from a class into a dictionary
Get values from a class into a dictionary

Time:11-25

I have an object volresult containing a list of IDs (int), Date (DateTime), and prices (double).

I want to use those values in another project and to avoid circular references when passing object volresult I want to pass Dates and Prices as a Dictionary<DateTime, Double>.

I have tried:

var try1 = results.values.Select(x => new {x.Date, x.Prices}).ToDictionary(DateTime, Double);
var try2 = results.values.ToDictionary<DateTime, Double>(x => new {x.Date, x.Prices});
var try3 = results.values.SelectMany(x => x.Date, y => y.Prices});

And some other derivations but can't make it work.

CodePudding user response:

Use the overload of ToDictionary with two Funcs as arguments. The first Func will select your key, the second Func will select the value.

var dictionary = result.values.ToDictionary(x => x.Date, x => x.Prices);

See also the documentation.

  • Related