Home > front end >  C#: Get the sum of the value passed to this list
C#: Get the sum of the value passed to this list

Time:10-12

I'm creating a daily report of expenses and incomes, and I would like to return the total I pass to a list. But I'm failing.

Here's the main part.

Class "Movimento", which defines the expense or the incomes themselves.

    namespace Agenda_personale
{
    internal class Movimento
    {
        public double Totale;
        public DateTime Data;

        //public bool IsSpesa()
        //{
        //if (Totale < 0) { return true; } // se è minore di 0 registra un negativo, quindi una perdita
        //else { return false; }
        //}
        public bool IsSpesa() => Totale < 0;
    }
}

And finally, the Bilancio class, which includes the methods to be used to get the expenses, their dates, etc..

internal class Bilancio3
{

    private List<Movimento> singoleSpese = new List<Movimento>(); // le liste devono stare private
    private List<Movimento> ReportDelMese = new List<Movimento>();
    private List<Movimento> ReportDelGiorno = new List<Movimento>();
    public double SaldoResiduo()
    {
        double totale = 0;
        for (int i = 0; i < singoleSpese.Count; i  ) // singoleSpese è legato a RegistraSpese che aggiunge spese (e date) individuali
        {
            Movimento spesa = singoleSpese[i];
            totale = totale   spesa.Totale; // .Totale è una proprietà
        }

        Console.WriteLine(totale);
        return totale;
    }

    public void tutteLeSpese()
    {
        foreach (Movimento s in singoleSpese) // ci dà lui l'oggetto da scorrere
            Console.WriteLine(s.Totale   " "   s.Data);
    }

    public void RegistraMovimento(Movimento s)
    {
        this.singoleSpese.Add(s);
        return;
    }

    public void ReportGiornaliero()
    {
        for (int i = 0; i < singoleSpese.Count; i  )
        {
            int giorno = singoleSpese[i].Data.Day;
            int mese = singoleSpese[i].Data.Month;
            int anno = singoleSpese[i].Data.Year;
            DateTime oggi = DateTime.Now;
            if (giorno == oggi.Day)
            {
                if (mese == oggi.Month)
                {
                    if (anno == oggi.Year)
                    {
                        this.ReportDelGiorno.Add(new Movimento());
                    }
                }
            }
        }
        Console.Write(ReportDelGiorno.Sum(x => Convert.ToInt32(x)));
        return;
    }

    public void ReportMensile()
    {
        for (int i = 0; i < singoleSpese.Count; i  )
        {
            int mese = singoleSpese[i].Data.Month;
            int anno = singoleSpese[i].Data.Year;
            DateTime oggi = DateTime.Now;
            if (mese == oggi.Month)
            {
                if (anno == oggi.Year)
                {
                    this.ReportDelMese.Add(new Movimento());
                }
            }
        }
        Console.Write(ReportDelMese.Sum(x => Convert.ToInt32(x)));
        return;
    }

    public bool HaiSpesoTroppo()
        {
            if (SaldoResiduo() < 0)
            {
                return true;
            }
            return false;
        }
    }
}

Error I get is:

Unhandled exception. System.InvalidCastException: Unable to cast object of type 'Agenda_personale.Movimento' to type 'System.IConvertible'. at System.Convert.ToInt32(Object value) at Agenda_personale.Bilancio3.<>c.b__6_0(Movimento x) in C:\Users\gabri\source\repos\Agenda_personale\Agenda_personale\Bilancio3.cs:line 85 at System.Linq.Enumerable.Sum[TSource](IEnumerable1 source, Func2 selector) at Agenda_personale.Bilancio3.ReportGiornaliero() in C:\Users\gabri\source\repos\Agenda_personale\Agenda_personale\Bilancio3.cs:line 85 at Agenda_personale.Program.TestBilancio3() in C:\Users\gabri\source\repos\Agenda_personale\Agenda_personale\Program.cs:line 66 at Agenda_personale.Program.Main(String[] args) in C:\Users\gabri\source\repos\Agenda_personale\Agenda_personale\Program.cs:line 27

Interested in getting the total of ReportDelGiorno and ReportDelMese lists. Thanks in advance.

CodePudding user response:

ReportDelGiorno and ReportDelMese a lists of Movimento which does not implement IConvertible and can't be converted to int. Just sum Totale:

Console.Write(ReportDelGiorno.Sum(x => x.Totale));

Cast the result to int if needed:

Console.Write((int)ReportDelGiorno.Sum(x => x.Totale));

CodePudding user response:

Try first projecting your Movimento.Totale property (making an assumption here on what you're trying to sum up) using Select() before you call the Sum method, for example:

ReportDelGiorno.Select(m => m.Totale).Sum()

This should return the sum of your "Totale"s.

  • Related