This is a migrated thread and some comments may be shown as answers.

Calculated Columns Divide By 0

2 Answers 21 Views
GridView
This is a migrated thread and some comments may be shown as answers.
poet_byron
Top achievements
Rank 1
poet_byron asked on 07 Apr 2011, 10:24 PM
I'm trying to use the following expression to avoid division by 0:

 

 

Expression<Func<UPSReport, double>> expression = u => ((u.U1 + u.G1 + u.C1) > 0 ? (u.Cl / (u.U1 + u.G1 + u.C1)) : 0);

It keeps coming out with just 0's.  How could this be resolved?

 

2 Answers, 1 is accepted

Sort by
0
Yavor Georgiev
Telerik team
answered on 08 Apr 2011, 05:55 AM
Hello Matthew,

 I think your expression should work. However, could you please test it out manually using the following code:
Expression<Func<UPSReport, double>> expression = u => ((u.U1 + u.G1 + u.C1) > 0 ? (u.Cl / (u.U1 + u.G1 + u.C1)) : 0);
var func = expression.Compile() as Func<UPSReport, double>;
foreach (var item in myDataItemsSource)
{
     System.Diagnostics.Debug.WriteLine(func(item));
}

 If you get only zeroes in the Visual Studio Output window, then I'm afraid you'll have to debug your model. Also, even though I can't see an error in your expression, perhaps it'd be best to set a breakpoint and examine its DebugView property of the expression in the Visual Studio debug visualizer to see if the compiler has interpreted your intent correctly.

All the best,
Yavor Georgiev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
poet_byron
Top achievements
Rank 1
answered on 11 Apr 2011, 08:45 PM
This had to do with me not typecasting my variables to double:

Expression<Func<UPSReport, double>> expression = u => ((u.U1 + u.G1 + u.C1) > 0 ? (u.Cl / (u.U1 + u.G1 + u.C1)) : 0);

Changed to:

 

 

Expression<Func<UPSReport, double>> expression = ups => (ups.UPS + ups.Guests + ups.Cust_Svc) > 0 ? (double)ups.Closes / ((double)ups.UPS + (double)ups.Guests + (double)ups.Cust_Svc) : 0;

Now it works.  Thanks for your help.

 

Tags
GridView
Asked by
poet_byron
Top achievements
Rank 1
Answers by
Yavor Georgiev
Telerik team
poet_byron
Top achievements
Rank 1
Share this question
or