0
votes
@inherits ToSic.Sxc.Dnn.RazorComponent
@using System.Linq;

@{
    var Measures = AsList(App.Data["Measures"]);
}

<p>@Measures.Where(... something ...).Select(s => s.Time).Max()</p>
<p>@Measures.Where(... something ...).Select(s => s.Time).Min()</p>
<p>@Measures.Where(... something ...).Select(s => s.Time).Average()</p>

Max and min work. Average returns:

'System.Collections.Generic.IEnumerable<dynamic>' does not contain a definition for 'Average' and the best extension method overload 'System.Linq.Queryable.Average(System.Linq.IQueryable<int>)' has some invalid arguments 

Time is a numeric field. I assume I am missing some sort of cast?

2

2 Answers

1
votes

Min and Max are probably very generic, since even a string can have min/max.

Average really needs to be sure it's a number. My guess is this should fix what you're doing

<p>@Measures.Where(... something ...).Select(s => (double)s.Time).Average()</p>

This may cause trouble if s.Time is null, in which case you would want something like

<p>@Measures.Where(... something ...).Select(s => s.Time as double? ?? 0).Average()</p>

didn't try it, but it should probably work. Casting to double? means nullable-double, and then adding ?? 0 should make sure all nulls are 0.

1
votes

Ended with

var GetMeasures = Measures.Where(u => u.Category.CatName == c.CatName && !u.Time.Equals(null));
var getTimes = GetMeasures.Select(s => (double)s.Time);

if (getTimes.Count() > 0) {
    <p>@getTimes.Max()</p>
    <p>@getTimes.Min()</p>
    <p>@getTimes.Average()</tdp
}