Getting error in enumerable sum

I need to sum the enumerable data table and I am getting the following error.

21.4.4+Branch.master.Sha.a8b5101efe02651274064093e9c7410fedff7696

Source: Assign

Message: Conversion from string "" to type 'Double' is not valid.

Exception Type: System.InvalidCastException

RemoteException wrapping System.InvalidCastException: Conversion from string "" to type 'Double' is not valid.  ---> RemoteException wrapping System.FormatException: Input string was not in a correct format. 
   at Microsoft.VisualBasic.CompilerServices.Conversions.ParseDouble(String Value, NumberFormatInfo NumberFormat)
   at Microsoft.VisualBasic.CompilerServices.Conversions.ToDouble(String Value, NumberFormatInfo NumberFormat)
	--- End of inner exception stack trace ---
   at Microsoft.VisualBasic.CompilerServices.Conversions.ToDouble(String Value, NumberFormatInfo NumberFormat)
   at Microsoft.VisualBasic.CompilerServices.Conversions.ToDouble(Object Value, NumberFormatInfo NumberFormat)
   at lambda_method(Closure , DataRow )
   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
   at System.Linq.Enumerable.Sum(IEnumerable`1 source)
   at lambda_method(Closure , ActivityContext )
   at Microsoft.VisualBasic.Activities.VisualBasicValue`1.Execute(CodeActivityContext context)
   at System.Activities.CodeActivity`1.InternalExecuteInResolutionContext(CodeActivityContext context)
   at System.Activities.Runtime.ActivityExecutor.ExecuteInResolutionContext[T](ActivityInstance parentInstance, Activity`1 expressionActivity)
   at System.Activities.InArgument`1.TryPopulateValue(LocationEnvironment targetEnvironment, ActivityInstance activityInstance, ActivityExecutor executor)
   at System.Activities.RuntimeArgument.TryPopulateValue(LocationEnvironment targetEnvironment, ActivityInstance targetActivityInstance, ActivityExecutor executor, Object argumentValueOverride, Location resultLocation, Boolean skipFastPath)
   at System.Activities.ActivityInstance.InternalTryPopulateArgumentValueOrScheduleExpression(RuntimeArgument argument, Int32 nextArgumentIndex, ActivityExecutor executor, IDictionary`2 argumentValueOverrides, Location resultLocation, Boolean isDynamicUpdate)
   at System.Activities.ActivityInstance.ResolveArguments(ActivityExecutor executor, IDictionary`2 argumentValueOverrides, Location resultLocation, Int32 startIndex)
   at System.Activities.Runtime.ActivityExecutor.ExecuteActivityWorkItem.ExecuteBody(ActivityExecutor executor, BookmarkManager bookmarkManager, Location resultLocation)

Flow will be like :
For Each
{
Assign activity contains
dTotalSum=dTotalSum+ dbPentruCalcule.AsEnumerable.Sum(Function(x)CDbl(x(iCounter)))
iCounter = iCounter+1
}

Where dTotalSum =0
dbPentruCalculate will be like


iCounter=0

@ppr
Note : This has been worked fine for few don’t know why this error occurs

HI @Sudharsan_AIT ,
Try below code
dTotalSum=Math.Round(dbPentruCalcule.AsEnumerable.Sum(Function (x) double.Parse(x.Item("Column2").ToString),0),2)

Regards,
Arivu

I am getting the error.

Input is not in the correct format

Try below code
Math.Round(dbPentruCalcule.AsEnumerable.Sum(Function (x) If(Double.TryParse(x.item("Column2").ToString, Nothing), Double.Parse(x.item("Column2").ToString),0)),2)

Regards,
Arivu

Hi,

If you want to sum all items, the following will work.

total = dbPentruCalcule.AsEnumerable.SelectMany(Function(r) r.ItemArray).Sum(Function(x) if(Double.TryParse(x.ToString,New Double),Double.Parse(x.ToString),0))

Regards,

Can you send me the code in this format
dTotalSum=dTotalSum+ dbPentruCalcule.AsEnumerable.Sum(Function(x)CDbl(x(iCounter)))

Because I need to use counter to separate the values

Hi,

How about the following?

dTotalSum = dTotalSum+ dbPentruCalcule.AsEnumerable.Sum(Function(x) if(Double.TryParse(x(iCounter).ToString,New Double),Double.Parse(x(iCounter).ToString),0))

Regards,

1 Like

Will try this and let you know @Yoichi

@Yoichi it is working for me.

1 Like