c# - 如何获得符合条件的通用列表元素的平均值?

标签 c# linq

我想重新创建 excel 函数 AVERAGEIF 的通用版本。它的定义如下:

Returns the average (arithmetic mean) of all the cells in a range that meet a given criteria

我的问题是我不知道如何定义通用平均函数的类型签名。以下是我拥有的几乎可以工作的代码。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsTestApp
{
    class Program
    {
        static void Main(string[] args)
        {
            var dl = new List<double>() { 10, 10, 0, 0 };
            var bl = new List<bool>() { true, true, false, false };
            Func<bool, bool> criteria = c => c == true;

            Console.Out.WriteLine(AVERAGEIF<double, bool>(dl, bl, criteria));
            Console.In.Read();
        }

        static T1 AVERAGEIF<T1, T2>(List<T1> average_range, List<T2> criteria_range, Func<T2, bool> applyCriteria)
        {
            var cl1 = new List<Container<T1>>();
            foreach (var cl in average_range)
            {
                cl1.Add(new Container<T1>(cl));
            }
            var cl2 = new List<Container<T2>>();
            foreach (var cl in criteria_range)
            {
                cl2.Add(new Container<T2>(cl));
            }

            List<Container<T1>> result =
                new List<Container<T1>>((from d in cl1
                                         join b in cl2
                                         on d.Id equals b.Id
                                         where applyCriteria(b.Value)
                                         select new Container<T1> { Value = d.Value, }).ToList<Container<T1>>());

            var ret = result.Average<T1>(s => s.Value);
            //
            //  return ret;
            return default(T1);
        }
    }

    class Container<T>
    {
        private static uint count = 0;

        public Container()
            : base()
        {
            count++;
            this.Id = count;
        }

        public Container(T t)
            : this()
        {
            this.Value = t;
        }

        public T Value
        {
            get;
            set;
        }

        public uint Id
        {
            get;
            private set;
        }
    }

    class ContainerList<T> : List<Container<T>>
    {
        List<Container<T>> m_list;

        public ContainerList()
            : base()
        {
            this.m_list = new List<Container<T>>();
        }

        public ContainerList(List<T> l)
            : this()
        {
            foreach (var li in l)
            {
                this.m_list.Add(new Container<T>(li));
            }
        }

        public ContainerList(List<Container<T>> l)
            : this()
        {
            this.m_list = l;
        }

        public T Average(Func<Container<T>, T> selector)
        {
            T ret = default(T);


            if (typeof(T) == typeof(double))
                ret = (T)(object)(double)0.0;
            else if (typeof(T) == typeof(decimal))
                ret = (T)(object)(decimal)0.0;
            else
                ret = default(T);

            return ret;
        }
    }
}

最佳答案

I'd like to recreate a generic version of the excel function AVERAGEIF

为什么不直接使用 LINQ?

var average = collection.Where(x => x.Something)
                        .Average(x => x.SomeProperty);

请注意,如果没有匹配的元素,这将抛出异常。如果你不想这样,你可以使用:

var average = collection.Where(x => x.Something)
                        .DefaultIfEmpty()
                        .Average(x => x.SomeProperty);

不清楚为什么要为此创建一个单独的方法。

关于c# - 如何获得符合条件的通用列表元素的平均值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14759452/

相关文章:

c# - 从三个非唯一的 List<T> 中获取一个唯一的 List<T>

c# - 我如何通过 postgres 加密并通过 c# 解密?

c# - Entity Framework 拆分表删除

c# - 在 IEnumerable 扩展中 - 为什么只有 Count() 针对 ICollection 进行了优化?

c# - 对 IEnumerable 中的先前值求和

c# - LINQ 中的股票数据结转到第二天

C# Linq - 连接具有多个字段的表时出现问题 - 错误 CS1941

java - 如何衡量C#和Java内存使用的差异?

c# - System.NotSupportedException - 无法比较“System.Linq.IQueryable”类型的元素

c# - 为列表 c# 中的特定索引应用 lambda 表达式