Click here to Skip to main content
15,868,145 members
Please Sign up or sign in to vote.
2.33/5 (3 votes)
See more:
this is what i wanna reach:
C#
foreach (string i in lijst) and (int a in aantalLijst){}


What I have tried:

foreach (string i in lijst) and (int a in aantalLijst)
Posted
Updated 9-Jun-16 10:43am
Comments
Sergey Alexandrovich Kryukov 9-Jun-16 15:53pm    
No.
—SA
Patrice T 9-Jun-16 16:03pm    
Describe what would be the effect of it.
Sergey Alexandrovich Kryukov 9-Jun-16 16:07pm    
Exactly. Good suggestion.
—SA
BillWoodruff 9-Jun-16 19:34pm    
I think Matt Hefron's answer below is probably what you need, but I am curious to ask you: do you know for sure that both Lists are the same size; if so, why not use a 'For loop ?

A big assumption:

You want to walk the two lists in parallel (each iteration takes the next value from lijst and the next value from aantalLijst.

If this is the case, you can do this with Linq using the Zip() method:
C#
using System.Linq;
// Two ways:
// #1:
foreach (var pair in lijst.Zip(aantalLijst, (ii, aa) => new { i = ii, a = aa }))
{
  string i = pair.i;
  int a = pair.a;
  // rest of loop statements
  // You could also just substitute pair.i and pair.a for i and a in the loop statements
}

// #2: (especially if the foreach was constructing some value)
var result = lijst.Zip(aantalLijst, (i, a) => {
                         // loop statements referencing i and a
                       });

Notes: in #2

  • The lambda expression for the loop body MUST return some value from each iteration, even if you don't use it (this is to resolve the required types).
  • The .Zip() returns an IEnumerable<T> where T is the type of the lambda return value.
  • The returned IEnumerable<T> as result will be lazy evaluated, so nothing will happen until result is iterated-over.
  • If you don't need any of the return values, then I suggest the lambda return true, and then result.All(); will iterate over the values.
  • If you do want to collect individual returned values, then you probably should use either result.ToList() or result.ToArray() depending on the desired form of the collection of values.
 
Share this answer
 
v2
Comments
Maciej Los 9-Jun-16 17:39pm    
Zip'em all!
5!
Matt T Heffron 9-Jun-16 18:33pm    
Thank you.
Member 12491690 9-Jun-16 19:04pm    
thank you this solved it for me
Matt T Heffron 9-Jun-16 19:14pm    
You're welcome!
(Out of curiosity: Which way of using .Zip() did you choose?)
BillWoodruff 9-Jun-16 19:32pm    
+5 an excellent and reasonable inference of the OP's intention.
Wrong idea; there is no such thing. Also, even it something like that was possible, it would not make any sense.
You did not show any relationship between lijst and aantalLijst. They are just different instances of System.IEnumerable, two unrelated arrays or collections. Note that foreach loops are based on this interface. Please see:
IEnumerable Interface (System.Collections).

You can have two different unrelated loops. The cannot even be nested, unless you want to search for something in one collection/array, depending on current element of another collection/array.

—SA
 
Share this answer
 
v3
Comments
Maciej Los 9-Jun-16 16:26pm    
5ed!
Sergey Alexandrovich Kryukov 9-Jun-16 16:29pm    
Thank you, Maciej.
—SA
Matt T Heffron 9-Jun-16 19:17pm    
Yes ... there is no foreach syntax for the construct OP presented, but solving for the apparent intent is actually pretty nice with Linq.
See my Solution 2.
Sergey Alexandrovich Kryukov 9-Jun-16 19:56pm    
Sure, it elegant, but I, frankly, cannot understand how did you infer the formulation of the problem you have solved/demonstrated, from the inquirer's post. I would understand if there was some mention that for each pair of elements of two different IEnumerable collections, of potentially different type, some action, depending on the pair, should be called. But there is nothing, no hint. Well, you can speculate that it would be the only reasonable motivation for the question, but... isn't it going to far, into the uncertain?

As to the generic algorithms like Enumerable.Zip... Isn't it already too much? What is that fundamental importance? Well, that's all for the functions on a Cartesian square, I understand, but the interpretation of fully abstract, and still, exactly two arguments (yes, Cartesian, that is, relationship between two collections can be defined on that). Number 2 as a world's universal constant... And the algorithm is not optimized. For more ad-hoc solutions, one could skip some elements, but no, in the "universal" algorithm, the function is always called N x M times... I mean, I would think twice before adding such function to the library. I mean it, I developer Cartesian square support in my article on Enumeration, but I did not come to the idea to create one universal enumeration by the set of pairs. After all, why should we consider application developers as kind who can only call functions from the library?..

Note that what some consider as a virtue, inference from the inquirer's question, I rather consider as some problem. Truly, I really think it's better not to understand too much and not trying to read between lines. Anyway, I appreciate your inventiveness in this respect. :-) It's just that I'm not completely sure that your assumption of the intent is correct.

I must admit, several times, I've been criticized for assuming too my myself. And you know, I often accepted such criticism; I just failed to see other possible interpretations. So, I'm trying to be a bit more careful with such assumptions...

—SA
Matt T Heffron 9-Jun-16 20:26pm    
Agreed that assumptions can be way off!
I attempted to cover myself for that case with the first line disclaimer! ;-)

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