Smith
Smith

Reputation: 5959

Converting a function to vb from csharp

I converted this code using the converter.telerik.com

From

public Decimal CartTotal()
{
   return this.Items.Sum<CartItem>((Func<CartItem, Decimal>) (x => x.Total));
}

To

Public Function CartTotal() As Decimal
   Return Me.Items.Sum(Of CartItem)(CType((Function(x) x.Total), Func(Of CartItem, Decimal)))
End Function 

but the compiler says

Overload resolution failed because no accessible 'Sum' accepts this number of type arguments.

Upvotes: 0

Views: 133

Answers (2)

Dave Doknjas
Dave Doknjas

Reputation: 6542

You simply need to drop the generic specifier on 'Sum':

Public Function CartTotal() As Decimal
   Return Me.Items.Sum(CType(Function(x) x.Total, Func(Of CartItem, Decimal)))
End Function

Exactly why VB has a problem with this escapes me at the moment, but it's a common problem. In general, the issue affects usage of any of the IEnumerable extension methods.

Upvotes: 0

Frank Alvaro
Frank Alvaro

Reputation: 487

Is this a question about what it should be, or one about Telerik's converter? If the latter, then you'd probably want to take it up with Telerik...

If the former, then maybe

Return (from item as CartItem in items Select item.Total).Sum()

.NET Fiddle

Upvotes: 0

Related Questions