微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在 2sxc 数字字段中使用 average()

如何解决在 2sxc 数字字段中使用 average()

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

最大和最小工作量。平均回报:

'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 

时间是一个数字字段。 我想我错过了某种演员吗?

解决方法

Min 和 Max 可能非常通用,因为即使是字符串也可以有 min/max。

Average 确实需要确保它是一个数字。我的猜测是这应该可以解决您正在做的事情

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

如果 s.Time 为空,这可能会导致问题,在这种情况下,您需要类似

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

没试过,不过应该可以。强制转换为 double? 意味着可空双精度,然后添加 ?? 0 应确保所有空值为 0。

,

结束于

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
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。