Click here to Skip to main content
15,888,019 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
<pre>lblSub.Text =
               lbDiscount.Items.Zip(lbPrice.Items)
               .Select((discount, price) =>
                 Convert.ToDecimal(discount) * Convert.ToDecimal(price))
               .Sum()
               .ToString();


i get a warning for the zip saying "No overload for method 'Zip' takes 1 arguments" why is it happening and how do i fix it

What I have tried:

I tried to remove the zip function altogether but it didnt fixx much, for context i am trying to multiply to different listboxes and output the result into another listbox
Posted
Updated 10-Jun-20 2:24am

1 solution

The Zip overload which doesn't require a projection function was added in .NET Core 3.0. If you're using an earlier version of .NET Core, or if you're using .NET Framework, you have to provide a function which takes the two source elements and combines them into a single result element.

In this case, you just need to combine Zip and Select:
C#
blSub.Text = lbDiscount.Items
    .Zip(lbPrice.Items, (discount, price) => Convert.ToDecimal(discount) * Convert.ToDecimal(price))
    .Sum()
    .ToString();
Enumerable.Zip Method (System.Linq) | Microsoft Docs[^]
 
Share this answer
 
Comments
vikil chandrapati 10-Jun-20 18:05pm    
now it just comes up with an error saying ''ListBox.ObjectCollection' does not contain a definition for 'Zip' and no accessible extension method 'Zip' accepting a first argument of type 'ListBox.ObjectCollection' could be found (are you missing a using directive or an assembly reference?)'
Richard Deeming 11-Jun-20 4:50am    
ListBox.ObjectCollection[^] is a non-generic collection. You'll need to cast the items to a suitable type first.

For example:
lblSub.Text = lbDiscount.Items.Cast<object>()
    .Zip(lbPrice.Items.Cast<object>(), (discount, price) => Convert.ToDecimal(discount) * Convert.ToDecimal(price))
    .Sum()
    .ToString();

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900