I
I
Ilya Magdenko2017-04-28 17:48:35
C++ / C#
Ilya Magdenko, 2017-04-28 17:48:35

How to issue a query to the database if you need to be based on a date?

Look here is the code -

DateTime TodayDate = DateTime.Now.Date; // init    
    var query = from o in this.dbContext.RentCarDBSet
                                where (DbFunctions.TruncateTime(o.StartDate) >= TodayDate)
                                    orderby RentCarDB.StartDate
                                select o;
                    this.rentCarDBViewSource.Source = query.ToList();

Where i check date from StartDate in table is greater than today. This is ok, but I need to change the query so that not only the Start Date is checked, but also the Duration added to it from the same table.

Those. o.StartDate + o.Duration (where first is DateTime and second is String
(number of days)) >= TodayDate

How can this be written in a request?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sumor, 2017-04-28
@Sumor

The number of days as a string is, of course, lovely.
At least it is necessary to know that for a DBMS.
SQLServer has a DATEADD(datepart , number , date) function. In MySql, the DATE_ADD(date,INTERVAL expr type) function.
Convert the string with the number of days to a number, then add using the specified functions to today's date.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question