Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat's the pattern to use for iterating over associated sets of values?
    primarykey
    data
    text
    <p>It's pretty common - especially as you try to make your code become more data-driven - to need to iterate over associated collections. For instance, I just finished writing a piece of code that looks like this:</p> <pre><code>string[] entTypes = {"DOC", "CON", "BAL"}; string[] dateFields = {"DocDate", "ConUserDate", "BalDate"}; Debug.Assert(entTypes.Length == dateFields.Length); for (int i=0; i&lt;entTypes.Length; i++) { string entType = entTypes[i]; string dateField = dateFields[i]; // do stuff with the associated entType and dateField } </code></pre> <p>In Python, I'd write something like:</p> <pre><code>items = [("DOC", "DocDate"), ("CON", "ConUserDate"), ("BAL", "BalDate")] for (entType, dateField) in items: # do stuff with the associated entType and dateField </code></pre> <p>I don't need to declare parallel arrays, I don't need to assert that my arrays are the same length, I don't need to use an index to get the items out.</p> <p>I feel like there's a way of doing this in C# using LINQ, but I can't figure out what it might be. Is there some easy method of iterating across multiple associated collections?</p> <p><strong>Edit:</strong></p> <p>This is a little better, I think - at least, in the case where I have the luxury of zipping the collections manually at declaration, and where all the collections contain objects of the same type:</p> <pre><code>List&lt;string[]&gt; items = new List&lt;string[]&gt; { new [] {"DOC", "DocDate"}, new [] {"CON", "ConUserDate"}, new [] {"SCH", "SchDate"} }; foreach (string[] item in items) { Debug.Assert(item.Length == 2); string entType = item[0]; string dateField = item[1]; // do stuff with the associated entType and dateField } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload