yashaka
yashaka

Reputation: 836

map function to operate on two collections in c#

I need a map function to operate on two collections (sequences), something like this:

map((col1Item, col2Item) => { col1Item.text.contains(col2Item); }, col1, col2) 

I know C# has Enumerable.Select, but it accepts one collection. Are there "more than one collection" alternative?

UPDATE:

My current solution is something like the following:

if (elements.Select ((element, index) => { element.Text.Contains (expectedTexts[index]); }).All ( res => res == true)) { // do something if texts of each element from elements seq contains correspondent text from expectedTexts seq }

Though I am still curious about more classic "functional style" alternative.

Upvotes: 1

Views: 214

Answers (1)

akton
akton

Reputation: 14386

I think you are looking for IEnumerable.Zip. This takes two IEnumerable instances, applies a function to the elements at the same index of each, producing an collection of the results. For example:

IEnumerable<string> a;
IEnumerable<string> b;

// result will contain the concatenated strings
IEnumerable<string> result = a.Zip(b, (fromA, fromB) => fromA + fromB);

Upvotes: 2

Related Questions