Click here to Skip to main content
15,899,006 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
var db = new XDataContext();
var q = from t in db.Posts select t;
var to = new DateTime(2011, 2, 1).AddMilliseconds(-1);          
q = q.Where(n => n.Date <= to);
foreach (var qq in q)
	// I get FEBRUARY items here!!!



Why do I get February itmes her?? I Know, it could be done differently, but I'm puzzled why it fails when coded this way. Note: types involved here are all datetime.

Thanks

Julio
Posted
Updated 27-Jul-11 7:14am
v2
Comments
Joris Janssens 27-Jul-11 15:18pm    
You shouldn't get items from feb 2011 or later with that code. One question though: are the february items you get from 2011 (which shouldn't happen) or from earlier years (totally normal)?
Julio Kuplinsky 27-Jul-11 16:05pm    
from 2011
Joris Janssens 27-Jul-11 16:30pm    
very weird, I replicated your code and it's working normally here.

1. does "Console.WriteLine(to.ToString("yyyy-MM-dd HH:mm:ss"));" outputs "2011-01-31 23:59:59"
2. does it work normally when you replace "q = q.Where(n => n.Date <= to);" with the following code:
List<datetime> oldDates = new List<datetime>();
foreach (DateTime n in q) {
if (n.Date <= to) {
oldDates.Add(n);
}
}
foreach (var qq in oldDates) {
//output
}
This also has the benefit that you can step through it to see where it goes horribly wrong. But again, I see now flaw in your code.
Julio Kuplinsky 28-Jul-11 11:18am    
1. 23:59:59 doesn't look right either, if you are subtracting one *MILLI*second.
2. I don't see that your code focuses on the problem. It seems to me that the SQL engine mishandles the conversion of the C# datatype DateTime.

What is the result you are expecting?

The DateTime struct show values in this format:
C#
DateTime(yyyy,mm,dd)


Since month is 2 in your code, it will show values from february.
 
Share this answer
 
You have initialized your DateTime variable with 01-Fab-2011.

Even if you substract 1 Millisecond, it still remains 01-Fab-2011.

Hence you are getting "February" items.

Make sure if you want to substract Days/Months or something else.
 
Share this answer
 
v2
Comments
Julio Kuplinsky 27-Jul-11 13:51pm    
1) Really! it remains 1-feb-2011 what time exactly?
2) why does it work different if i subtract one second?
Joris Janssens 27-Jul-11 15:12pm    
"Even if you substract 1 Millisecond, it still remains 01-Fab-2011. "

That's not correct. If you subtract one millisecond you end up with 2011-Jan-31 23:59:59. I would have used "DateTime to = new DateTime(2011, 2,1)" as end date and "n.Date < to" though, it's a bit cleaner.
 
Share this answer
 
v3
Comments
Julio Kuplinsky 27-Jul-11 14:51pm    
this doesn't help at all. there's nothing wrong with the c# datatypes. It's LINQ/SQL that returns the wrong records. In fact,if i subtract one second i dont get any februaries.
Monjurul Habib 27-Jul-11 18:23pm    
look at my last link.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900