Reputation: 67
I have a ms sql 2008 table with data from a weather datalogger.
table format
LogDate (nvarchar) 01/04/2010
LogTime (nvarchar) 10:00
LogMonth (nvarchar) APR
LogYear (nvarchar) 2010
Rain_Today (Double) 5.6
The daily reading for rain totals are at 08:59 the next day. So the table shows 5.6 mm for example: 03/04/2010 at 08:59, but this is actually the total for the previous day i.e. 02/04/2010. All the rain readings are a day out and the last day of the month's value is shown for the first day of the next month.
Is there a way to list the above rain values for the date 02/04/2010 instead of 03/04/2010? The date and time columns are in string format. This code works fine but the dates are a day out as above.
Edit Fixed the code from bitwise & to conditional &&
var q = from c in context.Wxlogs
where
c.LogYear == year && c.LogMonth == month && c.Time.Contains("08:59")
orderby c.LogDate
select new {c.LogDate, c.Rain_today};
table data
Apr 2010 01/04/10 09:00 0.0
Apr 2010 01/04/10 09:01 0.0
Apr 2010 01/04/10 19:02 23.7
Apr 2010 02/04/10 00:03 23.8
Apr 2010 02/04/10 08:59 23.8 -- time of reading
Hope this makes sense.
Any help would be appreciated.
Thanks Belinda
Upvotes: 1
Views: 258
Reputation: 67
I managed to work this one out!! A new field LogDate2 was added and LogDate was converted from Nvarchar to a DateTime datatype and populated with LogDate's values.
As some LINQ functions aren't available using Entity Framework e.g. LogDate.Day or LogDate.Month I used EntityFunctions which provided the ability to use CLR methods.
See EntityFunctions
So, the new code below works perfectly. It may not be the best or most efficent way to do it, but it provides the correct results.
var q = from c in context.Wxlogs
where
c.LogYear == year && c.LogMonth == mm && c.LogTime.Contains("08:59")
orderby c.LogDate2
let dm = EntityFunctions.AddDays(c.LogDate2, -1)
select new {dm, c.Rain_today};
A big thanks to everyone who steered me in the right direction
Upvotes: 1
Reputation: 877
Before going further. The "AND" clause is wrong, you are using a bitwise operator.
it should be && and not &
OR is it just a TYPO?
var q = from c in context.Wxlogs
where
c.LogYear == year && c.LogMonth == month && c.Time.Contains("08:59")
orderby c.LogDate
select new {c.LogDate, c.Rain_today};
Upvotes: 1