Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
358 views
in Technique[技术] by (71.8m points)

c# - How can I get the average of the elements of a generic list that meet a criteria?

I'd like to recreate a generic version of the excel function AVERAGEIF. It is defined as the following:

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

My problem is that I don't know how to define the type signature of the generic average function. The following is the almost working code that I have.

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;
        }
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

Why don't you just use LINQ?

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

Note that this will throw an exception if there are no matching elements. If you don't want that, you could use:

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

It's not clear why you would want to create a separate method for this.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...