-1
 var productNameTags = document.DocumentNode.SelectNodes(textBox3.Text);
                var priceTags = document.DocumentNode.SelectNodes(textBox2.Text);
                var codeNameAndPrice = productNameTags.Zip(priceTags, (n, w) => new { productNameTags = n, priceTags = w });
                int counter = 1;
                if (codeNameAndPrice != null)
                {
                    foreach (var nw in codeNameAndPrice)
                    {
                         label1.Visible = true;
                         label1.Text += counter + ". " + nw.productNameTags.InnerHtml + " - " + nw.priceTags.InnerHtml + "\n";
                    }
                }

I have this code which looks at html tags and prints out a product name and a price from a website and prints out like this using .Zip:

  1. Baseball - £5.00
  2. Football - £10.00
  3. Toy Car - £15.00

Is there a simple way of adding three or more variables to zip together using a different method?

e.g.

  1. Baseball - £5.00 - 1123
  2. Football - £10.00 - 1124
  3. Toy Car - £15.00 - 1125

Thanks in advance!

TeaAnyOne
  • 477
  • 1
  • 9
  • 16
  • You can chain `Zip` commands - where to the 1123, 1124, and 1125 come from? – D Stanley Jan 06 '16 at 17:03
  • Possible duplicate of [Zip N IEnumerables together? Iterate over them simultaneously?](http://stackoverflow.com/questions/3989319/zip-n-ienumerablets-together-iterate-over-them-simultaneously) – Reg Edit Jan 06 '16 at 17:03
  • @RegEdit desired outcome of these two questions are different, so I'm not sure that it should be duplicate... On other hand answers can be adapted to answer this one... So keeping my gold-hammer in pocket instead of closing. – Alexei Levenkov Jan 06 '16 at 17:14

2 Answers2

1

I don't think you can do it easily with .Zip (bar multiple levels of zip?), but you could do it dynamically:

var names = new[] { "Baseball", "Football", "Toy Car" };
var prices = new[] { 5.0M, 10.0M, 15.0M };
var ids = new[] { 1123, 1124, 1125 };
var products = names
    .Select((name, index) => new { Name = name, Price = prices[index], Id = ids[index] });
TVOHM
  • 2,740
  • 1
  • 19
  • 29
  • 1
    Good point on ability to use index to merge (for sequences of the same length). Note that indexing suggestion works fine with array/List, but for Xml/Html node lists it will likely be O(n) rather than O(1) for every access to the index thus making overall code O(n^2) instead of O(n). – Alexei Levenkov Jan 06 '16 at 17:04
0

No.

You can chain Zip calls, or you can write (or find) extension/helper method that would walk multiple sources in parallel similar to Zip to provide functionality you are looking for.

In your particular case consider iterating over some parent nodes and selecting individual child nodes relative to parent (assuming all nodes for particular item are children of single "parent" node).

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179