Waheed
Waheed

Reputation: 10216

How to use DateTime in WHERE clause (LINQ)?

How to add date is where clause. I am using following code.

    var opportunites = from opp in this.DataContext.Opportunities
                            join org in this.DataContext.Organizations on opp.OrganizationID
                            equals org.OrgnizationID
                            select new
                            {
                                org.CreatedDate,
                                org.OrganizationName
                            };
     if (createdDate != string.Empty)
         opportunites = opportunites.Where(opp => 
                            opp.CreatedDate == Convert.ToDateTime(createdDate));

But with this code i am not returning any result. I think the error is with == operator.

I think i should have code like this

"where createdDate between" + createdDate + "00:00:00" + and createdDate + "23:59:59"

How to archive this thing in LINQ.

Any suggestion??

Upvotes: 2

Views: 11108

Answers (2)

Gerardo Charnichart
Gerardo Charnichart

Reputation: 1

You can add a new date to the criterion, just adding 86399 seconds:

DateTime dtFechaIni = Convert.ToDateTime(filter[0]);
DateTime dtFechaEnd = Convert.ToDateTime(filter[0]).AddSeconds(86399);

var getInfo = from gI in infEnt.tb_FileManagement_RegistrosArchivo
              where gI.tb_FileManagement_Archivo.FechaArchivo > dtFechaIni &&
              gI.tb_FileManagement_Archivo.FechaArchivo < dtFechaEnd
              select gI;

Upvotes: 0

Bruce
Bruce

Reputation: 8430

You can extract just the 'date' portion of a DateTime as follows:

opp.CreatedDate.Date == Convert.ToDateTime(createdDate).Date

Upvotes: 7

Related Questions