how to combine and convert two integer columns to datetime lambda c#

80 views Asked by At

i have 2 int column in sql database one for month and one for year i want to query this against Database like this DateFrom is dateTime and also DateTo is DataTime

   var result = GetAll().Where(x =>
        (x.InvoiceMonth >= DateFrom.Month && x.InvoiceYear >= DateFrom.Year)
                  &&
        (x.InvoiceMonth <= DateTo.Month && x.InvoiceYear <= DateTo.Year));

but the problem here if user sent 1/1/2021 to 1/1/2023
it will just return Jan 2021 and Jan 2022and Jan 2023

the correct return should return all things between 2 dates

1

There are 1 answers

1
J.Salas On

You can construct the date with the data you have

var result = GetAll().Where(x => 
     (new DateTime(x.InvoiceYear, x.InvoiceMonth, 1) >= 
      new DateTime(DateFrom.InvoiceYear, DateFrom.InvoiceMonth, 1))
              &&
    (new DateTime(x.InvoiceYear, x.InvoiceMonth, 1) <= 
      new DateTime(DateTo.InvoiceYear, DateTo.InvoiceMonth, 1)));