Steven Zack
Steven Zack

Reputation: 5114

linq how to query a specific date's data

I'm using Linq querying today's date. There is one column in my table called VisitTime which is a Datetime type.

I want to know how to write query statement to search today's data. Can anyone help me on this?

WebStatDataContext dc = new WebStatDataContext(_connString);

var query=
    from v in dc. VisitorInfors
    where v.VisitTime......
    select v

Upvotes: 2

Views: 645

Answers (2)

Akram Shahda
Akram Shahda

Reputation: 14781

Use DateTime.Now :

DateTime currentDate = DateTime.Now;
var query =    
    from v in dc. VisitorInfors    
    where v.VisitTime == currentDate.Date
    select v;

Upvotes: 0

jason
jason

Reputation: 241789

When working with DateTime.Now, you should always store it in a local variable otherwise you can get really nasty bugs from the clock changing between calls:

var now = DateTime.Now;
var query = from v in dc.VisitorInfors
            where v.VisitTime.Date == now.Date
            select v;

Upvotes: 3

Related Questions