locked
Load a static list dynamically RRS feed

  • Question

  • User539757411 posted

    I have the following classes:

    public class Year
        {
            public string description { get; set; }
            public List<Month> months { get; set; }
            public bool expanded { get; set; }
            public string cpfPessoa { get; set; }
            public string nomePessoa { get; set; }
            public string cargoPessoa { get; set; }
            public string matriculaPessoa { get; set; }
        }
    
        public class Month
        {
            public int num { get; set; }
            public string description { get; set; }
            public string ano { get; set; }
            public double totalRendimentos { get; set; }
            public double totalDesconto { get; set; }
            public double valorLiquido { get; set; }
            public List<FinancialReport> financialreport { get; set; }
            public bool expanded { get; set; }
        }
    
        public class FinancialReport
        {
            public string anoCompetencia { get; set; }
            public string monthCompetencia { get; set; }
            public int monthNumero { get; set; }
            public string nomePessoa { get; set; }
            public string matricula { get; set; }
            public string sqcontrato { get; set; }
            public string cpf { get; set; }
            public DateTime datacompetencia { get; set; }
            public string tipocalculo { get; set; }
            public string tipocategoria { get; set; }
            public string verba { get; set; }
            public string codigoverba { get; set; }
            public double valor { get; set; }
            public DateTime dataNascimento { get; set; }
            public string cargoPessoa { get; set; }
        }

    How would I load this lçist dinamically:

    List<Year> listaYear = new List<Year>() { new Year() { description = "2017", expanded = false, months = new List<Month>() {
    	new Month() { year="2017", expanded = false, num = 1, description = "JANUARY", financialreport = new List<FinancialReport>(){ }},
    	new Month() { year="2017", expanded = false, num = 2, description = "FEBRUARY", financialreport = new List<FinancialReport>(){ }},
    	new Month() { year="2017", expanded = false, num = 3, description = "MARCH", financialreport = new List<FinancialReport>(){ }},
    	new Month() { year="2017", expanded = false, num = 4, description = "APRIL", financialreport = new List<FinancialReport>(){ }},
    	new Month() { year="2017", expanded = false, num = 5, description = "MAY" , financialreport = new List<FinancialReport>() { }},
    	new Month() { year="2017", expanded = false, num = 6, description = "JUNE", financialreport = new List<FinancialReport>(){ }},
    	new Month() { year="2017", expanded = false, num = 7, description = "JULHO", financialreport = new List<FinancialReport>(){ }},
    	new Month() { year="2017", expanded = false, num = 8, description = "AUGUST", financialreport = new List<FinancialReport>(){ }},
    	new Month() { year="2017", expanded = false, num = 9, description = "SEPTEMBER", financialreport = new List<FinancialReport>(){ }},
    	new Month() { year="2017", expanded = false, num = 10, description = "OCTOBER", financialreport = new List<FinancialReport>(){ }},
    	new Month() { year="2017", expanded = false, num = 11, description = "NOVEMBER", financialreport = new List<FinancialReport>(){ }},
    	new Month() { year="2017", expanded = false, num = 12, description = "DECEMBER", financialreport = new List<FinancialReport>(){ }}
    } }, new Year() { description = "2018", expanded = false, months = new List<Month>() {
    	new Month() { year="2018", expanded = false, num = 1, description = "JANUARY", financialreport = new List<FinancialReport>(){ }},
    	new Month() { year="2018", expanded = false, num = 2, description = "FEBRUARY", financialreport = new List<FinancialReport>(){ }},
    	new Month() { year="2018", expanded = false, num = 3, description = "MARCH", financialreport = new List<FinancialReport>(){ }},
    	new Month() { year="2018", expanded = false, num = 4, description = "APRIL", financialreport = new List<FinancialReport>(){ }},
    	new Month() { year="2018", expanded = false, num = 5, description = "MAY" , financialreport = new List<FinancialReport>() { }},
    	new Month() { year="2018", expanded = false, num = 6, description = "JUNE", financialreport = new List<FinancialReport>(){ }},
    	new Month() { year="2018", expanded = false, num = 7, description = "JULHO", financialreport = new List<FinancialReport>(){ }},
    	new Month() { year="2018", expanded = false, num = 8, description = "AUGUST", financialreport = new List<FinancialReport>(){ }},
    	new Month() { year="2018", expanded = false, num = 9, description = "SEPTEMBER", financialreport = new List<FinancialReport>(){ }},
    	new Month() { year="2018", expanded = false, num = 10, description = "OCTOBER", financialreport = new List<FinancialReport>(){ }},
    	new Month() { year="2018", expanded = false, num = 11, description = "NOVEMBER", financialreport = new List<FinancialReport>(){ }},
    	new Month() { year="2018", expanded = false, num = 12, description = "DECEMBER", financialreport = new List<FinancialReport>(){ }}
    } } };

    Thanks in advance!

    Wednesday, June 27, 2018 12:18 PM

All replies

  • User753101303 posted

    Hi,

    "dynamically" is a bit vague. Do you mean you want to load this list when it is first used or that yol want to load this list from a db or that the year should adapt to the current year etc...

    Also keep in mind that static data are share by ALL users...

    Wednesday, June 27, 2018 12:31 PM
  • User539757411 posted

    Hi,

    "dynamically" is a bit vague. Do you mean you want to load this list when it is first used or that yol want to load this list from a db or that the year should adapt to the current year etc...

    Also keep in mind that static data are share by ALL users...

    Yes. I'd like to do a for loop for every month of the year of 2017, and current month of the current year, and initialize it with FinancialReport data.

    Wednesday, June 27, 2018 12:34 PM
  • User-1171043462 posted

    This way

     List<Month> month = new List<Month>();
            for (int i = 1; i <= 12; i++)
            {
               month.Add(new Month
                   {
                        year = "2017",
                        description = (new DateTime(2017, i, 1)).ToString("MMMM")
                   });
            }

    Wednesday, June 27, 2018 2:05 PM
  • User36583972 posted


    Hi xandeq,

    Yes. I'd like to do a for loop for every month of the year of 2017, and current month of the current year, and initialize it with FinancialReport data.

    The following code may can give your some help.

      protected void Button2_Click(object sender, EventArgs e)
            {
                List<Year> listaYear = returnListYear(new string[] { "2017", "2018" });
                Response.Write(listaYear.Count.ToString());
            }
    
            public List<Year> returnListYear(string[] years)
            {
                List<Year> listaYear = new List<Year>();
                for(int i=0; i <years.Length;  i++)
                {
                    Year ye = new Year();
                    ye.description = years[i];
                    ye.expanded = false;
    
                    ye.months= (new Func<List<Month>>(() => {
    
                        List<Month> month = new List<Month>();
                        for (int m = 1; m <= 12; m++)
                        {
                            month.Add(new Month
                            {
                                //year = "2017",
                                expanded = false,
                                num = m,
                                description = (new DateTime(Convert.ToInt32(years[i]), m, 1)).ToString("MMMM"),
                                financialreport = new List<FinancialReport>() { }
                            });
                        }
                        return month;
    
                    }))();
    
                    listaYear.Add(ye);
                }
                return listaYear;
                    
            }
    


    Best Regards,

    Yong Lu

    Thursday, June 28, 2018 6:34 AM