vb.net, select where statement
-
I don't seem to be getting this right in VB. I get an error in casting the result as an Integer, in which the result may not exist. ProductInfo_Inventory should be a join to get a count
Dim cStatus As Integer = context.ProductInfo_Inventory.Where(Function(m) m.partNumber = p_PartNumber).Select(Function(m) m.currentStatus).DefaultIfEmpty()
This is the whole thing. Not every item has a record in inventory for it's optional. So I'm not really sure how to properly handle this in VB My new program is much better and in c#, so I seem to be doing better in that now.
pValue = context.ProductInfo.Where(Function(m) m.PartNumber = p_PartNumber).Count()
Dim cStatus = context.ProductInfo_Inventory.Where(Function(m) m.partNumber = p_PartNumber).Select(Function(m) m.currentStatus).DefaultIfEmpty()
If (cStatus = 4) Then
dV = False
Else
dV = If(pValue > 0, True, False)
End If21st Century Globalism has become Socialism on a planetary scale, in which the unequal treaties of the past have come back into play.
-
I don't seem to be getting this right in VB. I get an error in casting the result as an Integer, in which the result may not exist. ProductInfo_Inventory should be a join to get a count
Dim cStatus As Integer = context.ProductInfo_Inventory.Where(Function(m) m.partNumber = p_PartNumber).Select(Function(m) m.currentStatus).DefaultIfEmpty()
This is the whole thing. Not every item has a record in inventory for it's optional. So I'm not really sure how to properly handle this in VB My new program is much better and in c#, so I seem to be doing better in that now.
pValue = context.ProductInfo.Where(Function(m) m.PartNumber = p_PartNumber).Count()
Dim cStatus = context.ProductInfo_Inventory.Where(Function(m) m.partNumber = p_PartNumber).Select(Function(m) m.currentStatus).DefaultIfEmpty()
If (cStatus = 4) Then
dV = False
Else
dV = If(pValue > 0, True, False)
End If21st Century Globalism has become Socialism on a planetary scale, in which the unequal treaties of the past have come back into play.
The first question is, what is the type of
currentStatus
?. If it's non-numeric or beyond the limits of integer, the cast will fail. Another thing is that if the current status is nullable (or reference type) then the default would be null which cannot be cast to integer. The most accurate information is in the error message, but you didn't post that. -
The first question is, what is the type of
currentStatus
?. If it's non-numeric or beyond the limits of integer, the cast will fail. Another thing is that if the current status is nullable (or reference type) then the default would be null which cannot be cast to integer. The most accurate information is in the error message, but you didn't post that.I had a customer walk in while posting and I hit post without much details, sorry about that; my bad. This just determines if an item really exist, in case someone messed with the query string and put bad chars in there, or if an item was marked for deletion. I went with this; cStatus is an Integer.
pValue = context.ProductInfo.Where(Function(m) m.PartNumber = p_PartNumber).Count()
If (context.ProductInfo_Inventory.Any(Function(m) m.partNumber = p_PartNumber)) Then
cStatus = context.ProductInfo_Inventory.Where(Function(m) m.partNumber = p_PartNumber).Select(Function(m) m.currentStatus).FirstOrDefault()
End IfIf (cStatus = 4) Then
dV = False
Else
dV = If(pValue < 4, True, False)
End If21st Century Globalism has become Socialism on a planetary scale, in which the unequal treaties of the past have come back into play.
-
I had a customer walk in while posting and I hit post without much details, sorry about that; my bad. This just determines if an item really exist, in case someone messed with the query string and put bad chars in there, or if an item was marked for deletion. I went with this; cStatus is an Integer.
pValue = context.ProductInfo.Where(Function(m) m.PartNumber = p_PartNumber).Count()
If (context.ProductInfo_Inventory.Any(Function(m) m.partNumber = p_PartNumber)) Then
cStatus = context.ProductInfo_Inventory.Where(Function(m) m.partNumber = p_PartNumber).Select(Function(m) m.currentStatus).FirstOrDefault()
End IfIf (cStatus = 4) Then
dV = False
Else
dV = If(pValue < 4, True, False)
End If21st Century Globalism has become Socialism on a planetary scale, in which the unequal treaties of the past have come back into play.
-
No problem! So everything works now? Just notice that FirstOrDefault may also return null which wuold cause an exception since cStatus is integer so depending on the situation you may want to use integer? and check if a value is actually returned.