Sheritlw
asked on
Linq to sql with select case
Hi EE,
I am trying to evaluate numbers for 0 before I divide the numbers in the linq query.
I know how to do a select case in a stored procedure but can't figure out how to do it in code behind VB.Net.
How do I check numbers for values of 0?
Thanks
I am trying to evaluate numbers for 0 before I divide the numbers in the linq query.
I know how to do a select case in a stored procedure but can't figure out how to do it in code behind VB.Net.
How do I check numbers for values of 0?
Thanks
Dim q = (From a In dc.ProductDetails Where a.ProductDetailID = iItemID _
Select New curstock = if((a.OuncesInStock = 0,a.OuncesInStock, 0) / if(a.OuncesPerItem = 0, 1))).FirstOrDefault
ASKER
Hi Kaufmed,
I received the error...
The IIF method returns two separate types: Int32, Decimal. Translation to SQL does not support different return types.
I did change, 1 to 1.0 but I still got the error. Both fields are of decimal type.
Any ideas?
Thanks
I received the error...
The IIF method returns two separate types: Int32, Decimal. Translation to SQL does not support different return types.
I did change, 1 to 1.0 but I still got the error. Both fields are of decimal type.
Any ideas?
Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi Kaufmed,
I tried the 1.0D but it changed to 1D.
I then tried creating two decimal variables but I still get the same error. I looked at the database and both values are 0.00. The table allows 0 since it is an inventory table.
I tried the 1.0D but it changed to 1D.
I then tried creating two decimal variables but I still get the same error. I looked at the database and both values are 0.00. The table allows 0 since it is an inventory table.
Dim decVal As Decimal = 1.0
Dim decZero As Decimal = 0
Dim q = (From a In dc.ProductDetails _
Where a.ProductDetailID = iItemID _
Select New With {.curstock = IIf(a.OuncesInStock = decZero, decVal, a.OuncesInStock) / IIf(a.OuncesPerItem = decZero, decVal, a.OuncesPerItem)}).FirstOrDefault()
Both fields are of decimal type.When you say "fields" are you referring to the DB columns, the type declaration of the Linq tables' columns, or both?
ASKER
Fields are the columns in the database. Both OuncesInStock and OuncesPerItem are of dbtype decimal.
Normally I can divide ouncesinstock by ouncesperite.
hmmm just a thought, maybe I should do a where clause specifying that both ouncesinstock and ouncesperitem > 0, if not wouldn't q return nothing?
Thanks
Normally I can divide ouncesinstock by ouncesperite.
hmmm just a thought, maybe I should do a where clause specifying that both ouncesinstock and ouncesperitem > 0, if not wouldn't q return nothing?
Thanks
Hi Sheritlw;
The IIf method return an Object type convert it to Decimal before using it, see code snippet.
Fernando
The IIf method return an Object type convert it to Decimal before using it, see code snippet.
Dim q = (From a In dc.ProductDetails _
Where a.ProductDetailID = iItemID _
Select New With { .curstock = a.OuncesInStock / Convert.ToDecimal(IIF(a.OuncesPerItem = 0, 1.0D, a.OuncesPerItem)) }).FirstOrDefault()
Fernando
So it excepted an object in place of a Decimal?
Open in new window