Retrieving data through LINQ to SQL
I got a weird problem here like I got a site that users can post comments on a friend profile page.Everytime a user post a comment my application sends e-mail to that page owner, you know to inform for a new posted comment on his/her profile page.The problem is I want to stop the application from sending email if that user has just recently posted a comment say like 5 hours ago/earlier.Here is the function I use that would try to check it:
Public Function CheckForNewPost(ByVal arg As String) As Boolean
Dim x As Integer = 0
Using dc As New WhatEverDataContext()
Dim newcomment = From mytable In dc.PostTable _
Where mytable.PostingUser.ToLower() = User.Identity.Name.ToLower() And mytable.PageOwner.ToLower() = arg.ToLower() And mytable.PostedDate.AddHours(5) >= DateTime.Now _
Select mytable
For Each comment In newcomment
开发者_Go百科 x = x + 1
Next
If x > 0 Then
'user has posted a comment recently
Return True
Else
Return False
End If
End Using
End Function
Then I use it like this:
Protected Sub Repeater1_ItemInserted(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewInsertedEventArgs) Handles Repeater1.ItemInserted
'send our mail
Dim PageOwner As String = Request.QueryString.Get("PageOwnerName")
If CheckForNewPost(PageOwner) = False Then
SendEMail(PageOwner)
End If
End Sub
But still the app still sending the mail even the user just posted 5 hours earlier. What do you think I'm doing here?
I think it will be clearer if you write your condition as
mytable.PostedDate <= DateTime.Now.AddHours(-5)
as the right hand side now reads as "Five Hours Ago."
So the entire condition now reads as My posted date is earlier than (or equal to) five hours ago.
You should be checking to see if mytable.PostedDate.AddHours(5) <= DateTime.Now. I think you just got it backwards.
mytable.PostedDate.AddHours(5) <= DateTime.Now
精彩评论