C# Group by each month

时间:2019-03-19 14:45:54

标签: c#

Here's a code.

  decimal[] men;

        for (b.Pradzia();b.Yra();b.Kitas()) // loops through editions
        {
            men = new decimal[13]; // 
            for (a.Pradzia();a.Yra();a.Kitas()) // loops through subscribers 
            {
                if (b.ImtiDuomenisL().Kodas == a.ImtiDuomenisP().Kodas) // if edition code matches subscriber code proceed
                {
                    int j = a.ImtiDuomenisP().LaikotarpioPradžia + a.ImtiDuomenisP().LaikotarpioIlgis; // gets the start of subscription +
                                                                                                       // the lenght of it.

                    for (int i = a.ImtiDuomenisP().LaikotarpioPradžia; i <= j; i++) 
                    {
                        Dictionary<Leidinys, decimal> suma = new Dictionary<Leidinys, decimal>();
                        if (j <= 12)
                        {
                            men[i] += a.ImtiDuomenisP().Kiekis * b.ImtiDuomenisL().Kaina;
                        }
                        else
                        {
                            men[j - 12] += a.ImtiDuomenisP().Kiekis * b.ImtiDuomenisL().Kaina;
                        }
                        suma.Add(b.ImtiDuomenisL(), men[i]); // adds the edition and the sum of it to the dictionary.
                    }
                }

            }


        }

What I get from this method is the sum of each edition in each month. Months are in integers for reasons.

For each month I need to determine, which edition got most money. I do not know how.

1 个答案:

答案 0 :(得分:0)

Assuming your "j" variable is months (Your code is a bit confused) you just need something like this:

int moneyofmonth = 0;
int biggest = 0;
foreach(var month in j)
{
     moneyofmonth = //moneyofthismonth using the var "month".
     if(moneyofmonth > biggest)
     {
        biggest = moneyofmonth;
     }
}

"biggest" will be the biggest money of all months. If you want so save the month of the biggest, it's just to make a new var inside if saving the "month" variable.