Search code examples
dotnetnuke2sxc

Using average() in 2sxc numeric field


@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?


Solution

  • 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
    }