Linq average of dynamic double

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Linq average of dynamic double



I have a dynamic result set, which contains a key value double (but the compiler still sees it as a dynamic). I am trying to take a cumulative average of this, but cannot figure out the correct cast or how to call the specific overload of the Linq Average() method. It consistently tries to call the Average<dynamic>(Func<dynamic, int> selector) version, when I am pretty sure I need the Average<dynamic>(Func<dynamic, double> selector) version.


Average()


Average<dynamic>(Func<dynamic, int> selector)


Average<dynamic>(Func<dynamic, double> selector)



Intellisense on the Average() and Sum() methods is what is showing me that the compiler is choosing the <dynamic, int> version. How can I specify to use the double version?


Average()


Sum()


<dynamic, int>


double



Here is a good sample of the iterations of things I have tried and their corresponding error. At this point, I feel I have tried every combination, and with or without Cast<double>() and Convert.ToDouble() that I can think of.


Cast<double>()


Convert.ToDouble()


// ## filled with values from DB query, but here with examples ##
var result = new List<dynamic>() new myDouble = 0.123 , new myDouble = 0.456 ;
var cumulativeAvg = new List<double>();



UPDATE My result set turned out to have a hidden null value, which caused the errors:


null


var result = new List<dynamic>()
new myDouble = 0.123
, new myDouble = 0.456
, new myDouble = null
;



For all of the following loops, the ERROR is:
Cannot implicitly convert type 'double' to 'int'


for(var i = 0; i < result.Count; i++)
cumulativeAvg.Add(result.Take(i + 1).Average(r => Convert.ToDouble(r.myDouble)));

for(var i = 0; i < result.Count; i++)
cumulativeAvg.Add(result.Take(i + 1).Select(r => Convert.ToDouble(r.myDouble)).Average());

for(var i = 0; i < result.Count; i++)
cumulativeAvg.Add(result.Take(i + 1).Select(r => Convert.ToDouble(r.myDouble)).ToList().Average());

for(var i = 0; i < result.Count; i++)
cumulativeAvg.Add(result.Take(i + 1).Sum(r => Convert.ToDouble(r.myDouble)) / (i + 1));



For all of the following loops, the ERROR is:
Specified cast is not valid.


for(var i = 0; i < result.Count; i++)
cumulativeAvg.Add(result.Take(i + 1).Select(r => Convert.ToDouble(r.myDouble)).Cast<double>().Average());

for(var i = 0; i < result.Count; i++)
cumulativeAvg.Add(result.Take(i + 1).Cast<double>().Select(r => Convert.ToDouble(r.myDouble)).Average());

for(var i = 0; i < result.Count; i++)
cumulativeAvg.Add(result.Select(r => Convert.ToDouble(r.myDouble)).Cast<double>().Take(i + 1).Average());





What is result? Is that a typo and should be results? But in that case, results can't contain a definition for myDouble.
– Sach
Aug 8 at 18:14


result


results


results


myDouble





Yes typo, now fixed. result is the dynamic result of a DB query, which contains several values, one of which is a double. I have updated with a sample value for clarification.
– chadiusvt
Aug 8 at 18:19


result




1 Answer
1



It is working if you force the cast of the dynamic to double instead of letting the .ToDouble method find the appropriate type itself. Instead, do an explicit cast:


.ToDouble


for(var i = 0; i < result.Count; i++)
cumulativeAvg.Add(result.Take(i + 1).Average(r => (double)r.myDouble));





I actually just tried this, and found the issue. In my specific case, one of the entries has hiding a null value, which is why the previous Cast() did not work. Using this explicit cast finally brought the issue to light.
– chadiusvt
Aug 8 at 18:35


null


Cast()






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS