Compare when a Variable becomes bigger than a Value in List/IEnumerable

Viewed 96

Background/Context

I am currently building a function that will take a value and compare it against a list to find at which point the string values become bigger. The value I take is a date and the String contains a range of dates that are headings to a chart and essentially I am trying to find the column in excel where this date would fit in. The heading dates are all weekly dates. I've tried to get my logic around it but nothing seems to be working so thought I'd ask on here.

Logic Behind Code

  1. The program creates a IEnumerable to find cell of records for which date we are looking up
  2. The program creates a IEnumerable with all the dates in the headings.
  3. The program takes that date and compares it to the IEnumerable in 2. to return a column where the new date will go.

Code up to point 2

List<string> Sheet1Rows = new List<string>();
List<string> Sheet2Rows = new List<string>();
List<DateTime> ColumnDates = new List<DateTime>();
//List<int> NewRowsNumber = new List<int>();

for (int row = 9; row <= LastRowSheet1; row++)
{
    Sheet1Rows.Add(ExcelWksht1.Cells[row, 1].Value);
}

for (int row = 9; row <= LastRowSheet2; row++)
{
    Sheet2Rows.Add(ExcelWksht2.Cells[row, 1].Value);
}

for (int column = 16; column <= LastColumnSheet2; column++)
{
    ColumnDates.Add(ExcelWksht2.Cells[5, column].Value);
}

IEnumerable<string> Names1 = Sheet1Rows;
IEnumerable<string> Names2 = Sheet2Rows;
IEnumerable<DateTime> Dates1 = ColumnDates;

IEnumerable<string> NamesInBoth = Names1.Intersect(Names2);
IEnumerable<string> NamesOnlyInTwo = Names2.Except(Names1);
Debug.Print("NamesInBoth\n==========");

foreach (var n in NamesInBoth)
{
    Debug.Print(n.ToString());

    //Get Row Values
    int rowinsheet1 = (Sheet1Rows.FindIndex(x => String.Compare(x, n) == 0)) + 9;
    int rowinsheet2 = (Sheet2Rows.FindIndex(x => String.Compare(x, n) == 0)) + 9;

    //Get Date Values
    var Sheet1Date = ExcelWksht1.Cells[rowinsheet1, 9].Value.ToString();
    var Sheet2Date = ExcelWksht2.Cells[rowinsheet2, 9].Value.ToString();

    if (Sheet1Date != "No Planned Date" && Sheet2Date != "No Planned Date")
    {
        if (Sheet1Date != null && Sheet2Date != null)
        {
            if (Sheet1Date.ToString() != Sheet2Date.ToString())
            {
                //THIS IS WHERE IM STRUGGLING
            }
        }
        else 
        {
            ((Excel.Range)ExcelWksht2.Cells[rowinsheet2, 1]).EntireRow.Interior.Color = Microsoft.Office.Interop.Excel.XlRgbColor.rgbOrange;
        }
    }  
} 

The Problem

As shown in the code I need to figure out a way to construct a loop (?) to check the row date value against all the heading values in the IEnumerable to give me the date value that begins the 7 day range.

0 Answers
Related