tags:

views:

365

answers:

2
query = query.Where(q => q.Date.ToString().Contains(strDate));

Above statement is not correct.

If I use "==" Then it will match exact date. That is O.K. but can I filter, Like we filter strings ??

+3  A: 

I don't really get what you mean with "filter".

You can do stuff like:

query = query.Where(q => q.Date.Year.Equals(myDate.Year));

or

query = query.Where(q => q.Date.Day.Equals(myDate.Day));

if that's what you're after.

Ofcourse you have to cast strDate to a DateTime. If you really don't want to do that, you can also cast the DB date to a string of the same format:

query = query.Where(q => q.Date.ToString("dd-MM-yyyy").Contains(strDate));
Thomas Stock
A: 

Assuming that q.Date is an instance of DateTime, I don't think converting a DateTime instance into a String is the best way to compare dates. It would be much more reliable to filter the DateTime based on properties on the object. For example, Year, Month, etc ...

For example if you wanted to filter on the year in strDate you could do the following.

var d = DateTime.Parse(strDate);
query = query.Where(q => q.Date.Year == d.Year);

Can you give us more context by detailing what type of values strDate would contain that way we can know what you want to filter against.

JaredPar