开发者

LINQ VB.net Return Single Type of Object Invalid Cast

Ok, just needing a 2nd set of eyes looking at this to make sure the error isn't something else other than my LINQ code here.  Here's the function class itself:

Public Function GetJacketByPolicyID(ByVal jacketID As Int32) As tblPolicy
   Dim db As New DEVDataContext()
   Dim j As tblPolicy = db.tblPolicies.Single(Funct开发者_JAVA技巧ion(p) p.policyNumber = jacketID)
   Return j
End Function

and here is the code which calls this class function in the web control form itself:

Dim p As tblPolicy
Dim j As New Jackets()
p = j.GetJacketByPolicyID(3000050)

For some reason it's flagging the 2nd line in the GetJacketByPolicyID function saying the specified cast is not valid.  So I'm guessing it's something I'm doing wrong.  I'm sure the tblPolicy/tblPolicies class works right since I can create a new instance of a tblPolicy and set a few variables by hand and return it, so that's not it.  I've also checked the datarow I'm fetching and there's no null values in the record, so that shouldn't be it either.

Any help much appreciated.


This would seem to get what you are looking for. Not sure why you are passing in a function for a simple query like this.

Public Function GetJacketByPolicyID(ByVal jacketID As Int32) As tblPolicy
 Dim _jacket as tblPolicy
 Using _db As New DEVDataContext()
  _jacket = (From _j In db.tblPolicies Where _j.policyNumber.Equals(jacketID) Select _j).Single()
 End Using
 Return _jacket
End Function
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜