I’ve noticed some irregularities while using linq in mvc3.
I have a database table with a “PIN” column. Column must have a 28 characters and completly random characters can be stored. Then I have a controller action:
Function Unlock() As ActionResult
Dim key As String = Request("token")
If key IsNot Nothing Then
Dim user As Users = db.Users.SingleOrDefault(Function(u) u.PIN = key)
If user IsNot Nothing Then
user.PIN = Nothing
db.SaveChanges()
Return View()
End If
End If
Return RedirectToAction("Index", "Home", New With {.Area = ""})
End Function
For example when PIN holds “Co/5c1mmil2e+clGK3c6JvdrGpQ=” key string reads that full string from querystring, but user reference always ends up nothing, even though that same value is stored in database.
On other hand when PIN holds “GbgI4QAaYWanaKWUm6j7Jg5IpA8=” everything works just fine. So I figured that maybe linq has problems with some characters like / or +, but how to solve this issue?
try
u.PIN.Equals(key)Use intelliTrace while debuging it will help you a lot with linq.
Also FirtsOrDefault seams more reasonable for me in this case instead of SingleOrDefault