Pergunta

I have a table in a SQL database that has the following fields: [Cid, Qid, Uid, score, Date]. The Cid doesn't matter though, but it is the primary key.

I need to get the weighted average of score Grouped By Qid and Uid. The weight is defined as (score)*(1.25^n), and n is the number of rows away from the first row that have the same Qid and Uid. [i.e. The first row with Qid and Uid will have a n of 0. The second row (the one with a later date but still the same Qid and Uid) will have a n of 1.]

So,

Qid    Uid   Date     BS
1       1    9/12/13  .5
1       2    9/13/13  .8
1       2    9/14/13  .5
1       2    9/15/13  .9
2       1    9/12/13  .75
2       1    9/13/13  .8
2       2    9/12/13  .75

The function should return:

Qid  Uid  WeightedAvg  (the weights are applied and then the average is taken)
1     1    .5
1     2    .94375
2     1    .5833
2     2    .75
Foi útil?

Solução

Try this out:-

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

namespace LinqToObjects
{
    class Custom
    {
        public int Qid { get; set; }
        public int Uid { get; set; }
        public DateTime Date { get; set; }
        public double BS { get; set; }
    }
    class Program
    {
        static void Main(string[] args)
        {
            List<Custom> lst = new List<Custom>()
            {
                new Custom(){ Qid=1, Uid=1, Date=new DateTime(2013,9,12) ,BS=0.5},
                new Custom(){ Qid=1, Uid=2, Date=new DateTime(2013,9,13) ,BS=0.8},
                new Custom(){ Qid=1, Uid=2, Date=new DateTime(2013,9,14) ,BS=0.5},
                new Custom(){ Qid=1, Uid=2, Date=new DateTime(2013,9,15) ,BS=0.9},
                new Custom(){ Qid=2, Uid=1, Date=new DateTime(2013,9,12) ,BS=0.75},
                new Custom(){ Qid=2, Uid=1, Date=new DateTime(2013,9,13) ,BS=0.8},
                new Custom(){ Qid=2, Uid=2, Date=new DateTime(2013,9,12) ,BS=0.75}
            };

            var query = lst.GroupBy(obj => new { obj.Qid, obj.Uid}, ( key,group) => new {Key1= key.Qid, Key2 = key.Uid, Avg = group.Average(o=>o.BS), lst = group});

            Console.WriteLine("Qid\tUid\tWeightedAvg");
            foreach(var item in query)
            {
                double weightAvg=0;
                int cnt = 0;
                foreach (var i in item.lst)
                {
                    weightAvg += i.BS * (Math.Pow(1.25, cnt++));
                }

                weightAvg /= item.lst.Count();

                Console.WriteLine(string.Format("{0}\t{1}\t{2}", item.Key1, item.Key2, weightAvg)) ;
            }

        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LinqToObjects
{
    class DateComparer : IEqualityComparer<Custom>
    {

        public bool Equals(Custom x, Custom y)
        {
            if (Object.ReferenceEquals(x.Date, y.Date)) return true;

            return false;
        }

        public int GetHashCode(Custom obj)
        {
            if (Object.ReferenceEquals(obj, null)) return 0;

            int hashCode = obj.GetHashCode();

            return  hashCode;
        }
    }
}

Output:-

Qid     Uid     WeightedAvg
1       1       0.5
1       2       0.94375
2       1       0.875
2       2       0.75
Press any key to continue . . .
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top