locked
Build CVS value to populate new string[] RRS feed

  • Question

  • User-1039663295 posted

    Hi all.

    I have the following code that works great when hard-coded:

    var headerRow = new List<string[]>()
    {
        new string[]{ "Team Closed Referrals", "", "", "Jan-18", "", "", "", "", "Feb-18", "", "", "", "" }
    };

    Now I need to change the following row from hard-coded to building it dynamically:

    new string[]{ "Team Closed Referrals", "", "", "Jan-18", "", "", "", "", "Feb-18", "", "", "", "" }

    This is due to the fact that I don't know what values will be present since it can be 1 month (Jan-18) or all 12 months.

    How can I build the list of values dynamically and also include the empty strings you see  above?  I need the empty strings to create separation between the headers when it gets placed in Excel.  I could add them later if necessary but would prefer not to. 

    Mark

    Thursday, October 4, 2018 3:24 PM

All replies

  • User753101303 posted

    Hi,

    So whenever you add an element you want to add then 4 empty strings ? Not sure to see what is the problem in coding this ?

    Do you mean you want something such as :

    var headerRow=GetHeaderRow(startDate,endDate); and it would return the needed list (with 2 empty strings after the first element and 4 after each other element ?) Or you want to create you own HeaderList type that would implement this behavior automatically on the Add method ?

    Sometimes rather than to start thinking about implementation details first, it is best to think about how you want to consume this from you main code.

    Thursday, October 4, 2018 4:12 PM
  • User-893317190 posted

    Hi meckeard,

    If you want to generate your array no matter how many months(up to 12) you have.You could try the code below.

     protected void Page_Load(object sender, EventArgs e)
            {
    
                string[] strs = GetOrganizedString("Oct-18","Feb-18","Jan-18");
            }
    
            private string[]  GetOrganizedString(params string[] origin)
            {
                List<string> list = new List<string>
                {
                    "Team Closed Referrals",
                    "",
                    ""
                };
              IEnumerable<string> strs=  origin
                  .Select(d => DateTime.ParseExact(d, "MMM-dd", CultureInfo.InvariantCulture))  //convert the string to datatime
                    .OrderBy(d => d) //  sort the strings by date
                    .Select(d => d.ToString("MMM-dd")); // convert the datetime back to string
                foreach (var item in strs)
                {
                    // add every element with 4 empty strings to the list
                    list.AddRange(new string[] { item, "", "", "", "" });
                }
                return list.ToArray();
            }

    The result.

    Since you haven't given the original data , it is hard to guess what you original data is.

    If it is not the case, please provide your original data and explain what result you want to get.

    Best regards,

    Ackerly Xu 

    Friday, October 5, 2018 2:36 AM