locked
compare list items and create objects for changesets RRS feed

  • Question

  • User713522837 posted

    I have the input the object as below

    var orderAvailability = new OrdderAvailabilty{

    StartDate = new DateTime(2019.5,21),

    EndDate = new DateTime(2019,5,27)

    Availability = new List<int>{0,0,0,1,0,0,0}

    }

    Each list item in the 'Availability' listed above corresponds to each date from StartDate and EndDate inclusive. For the above example, there are 7 list items and the date range is from 21st and 27th (7 days) i.e.,

    Availability on 21st is 0, 22nd is 0, 23rd is 0, 24th is 1, 25th is 0, 26th is 0 and 27th is 0. Starting from the 21st, there are 3 sets of availability

    availability from 21st till 23rd is 0

    availabily on 24th is 1

    availability from 25th till 27th is 0

    The required output should be

    From             | To                   | Available

    21/5/2019     | 23/5/2019     | 0

    24/5/2019     | 24/5/2019     | 1

    25/5/2019     | 27/5/2019     | 0

    Please could anyone help

    Tuesday, May 21, 2019 7:48 PM

All replies

  • User665608656 posted

    Hi ragavalli,

    According to your question, I suggest that you separate the contents of orderAvailability, divide the same numbers in the Availability array and put them into a new array.

    By looping through the new array, you can add the corresponding rows to the table of the final result.

    For more detailed code, please refer to the following code:

    <style type="text/css">
           .display th,
           .display td {
                border:none;
                border-right:1px solid black;
                height:25px;
            }
    </style>
    
    <form id="form1" runat="server">     
      <table id="example" class="display" style="width: auto; height: auto; text-align: center" runat="server" >
            </table>
    </form>

    code behind:

            protected void Page_Load(object sender, EventArgs e)
            {
                ShowData();
            }
             protected void ShowData()
             {
                var orderAvailability = new
                {
                    StartDate = new DateTime(2019, 5, 21),
                    EndDate = new DateTime(2019, 5, 27),
                    Availability = new List<int> { 0, 0, 0, 1, 0, 0, 0 }
                };
                List<int> ava = orderAvailability.Availability;
                DateTime stdate = orderAvailability.StartDate;
                DateTime eddate = orderAvailability.EndDate;
                //Create header rows
                HtmlTableRow trHead = new HtmlTableRow();
                //Create header cells
                HtmlTableCell tcHeadFrom = new HtmlTableCell();
                HtmlTableCell tcHeadTo = new HtmlTableCell();
                HtmlTableCell tcHeadAva = new HtmlTableCell();
                //Assign values to header cells
                tcHeadFrom.InnerHtml = "From";
                tcHeadTo.InnerHtml = "To";
                tcHeadAva.InnerHtml = "Available";
                trHead.Cells.Add(tcHeadFrom);
                trHead.Cells.Add(tcHeadTo);
                trHead.Cells.Add(tcHeadAva);
                example.Rows.Add(trHead);
    
                string temp = "";
                string tempList = "";
                List<string> aalists = new List<string>();
                for (int i = 0; i < ava.Count; i++)
                {
                    string am = ava[i].ToString();
                    if (temp != am)
                    {
                        if (temp != "")
                        {
                            aalists.Add(tempList);
                        }
                        temp = am;
                        tempList = am;
                    }
                    else
                    {
                        tempList += am;
                    }
                }
                aalists.Add(tempList);
                for (int i = 0; i < aalists.Count(); i++)
                {
                    HtmlTableRow tr1 = new HtmlTableRow();
                    HtmlTableCell tc11 = new HtmlTableCell();
                    HtmlTableCell tc12 = new HtmlTableCell();
                    HtmlTableCell tc13 = new HtmlTableCell();
                    int count = aalists[i].Count() - 1;
                    tc11.InnerHtml = stdate.ToString("dd/MM/yyyy");
                    tc12.InnerHtml = stdate.AddDays(count).ToString("dd/MM/yyyy");
                    tc13.InnerHtml = aalists[i].Substring(0, 1);
    
                    tr1.Cells.Add(tc11);
                    tr1.Cells.Add(tc12);
                    tr1.Cells.Add(tc13);
                    example.Rows.Add(tr1);
                    stdate = stdate.AddDays(count).AddDays(1);
                }
    
            }
    

    The result of my work demo:    

    Best Regards,

    YongQing.

    Wednesday, May 22, 2019 10:04 AM