1

I want to know that is there any alternative solution available in Dot Net 3.0 for Enumerable.Zip.

Here is the example that I want to implement:

I'm having two list of string.

var list1 = new string[] { "1", "2", "3" };
var list2 = new string[] { "a", "b", "c" };

I want to combine these lists, in such a way that it returns output like this:

{(1,a), (2,b), (3,c)}

I know, I can do this my using Zip in Dot Net >= 4.0. Using this way:

list1.Zip(list2, (x, y) => new Tuple<int, string>(x, y));

But, my problem is that I want to do the same in Dot Net 3.0. Is there any alternative method available in Dot Net <= 3.0 or I've to create custom method?

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
Saadi
  • 2,211
  • 4
  • 21
  • 50
  • 2
    Sure: http://referencesource.microsoft.com/#System.Core/System/Linq/Enumerable.cs,2b8d0f02389aab71 – Patrick Hofman Nov 07 '17 at 14:14
  • 2
    The ["duplicate"](https://stackoverflow.com/questions/2811822/does-linq-net3-5-support-a-zip-method) isn't one because there is no .NET 3 support. The [solution](http://blogs.msdn.com/ericlippert/archive/2009/05/07/zip-me-up.aspx) from Eric Lippert requires .NET 3.5 because of the `Func`. So i have reopened this question. – Tim Schmelter Nov 07 '17 at 15:03
  • @TimSchmelter Then add in a duplicate of how to define a delegate if the OP really doesn't know how to define a delegate. There's no reason to duplicate the implementation of the method. – Servy Nov 07 '17 at 15:08
  • Thank you everyone! – Saadi Nov 08 '17 at 08:55

1 Answers1

2

As you know it is not available in .NET 3.5 and older version of it. However there is an implementation of it by Eric Lippert here https://blogs.msdn.microsoft.com/ericlippert/2009/05/07/zip-me-up/:

The code to do so is pretty trivial; if you happen to need this in C# 3.0, I’ve put the source code below.

public static IEnumerable<TResult> Zip<TFirst, TSecond, TResult>
    (this IEnumerable<TFirst> first,
    IEnumerable<TSecond> second,
    Func<TFirst, TSecond, TResult> resultSelector)
{
    if (first == null) throw new ArgumentNullException("first");
    if (second == null) throw new ArgumentNullException("second");
    if (resultSelector == null) throw new ArgumentNullException("resultSelector");
    return ZipIterator(first, second, resultSelector);
}

private static IEnumerable<TResult> ZipIterator<TFirst, TSecond, TResult>
    (IEnumerable<TFirst> first,
    IEnumerable<TSecond> second,
    Func<TFirst, TSecond, TResult> resultSelector)
{
    using (IEnumerator<TFirst> e1 = first.GetEnumerator())
        using (IEnumerator<TSecond> e2 = second.GetEnumerator())
            while (e1.MoveNext() && e2.MoveNext())
                yield return resultSelector(e1.Current, e2.Current);
} 
Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
  • That is from http://referencesource.microsoft.com/#System.Core/System/Linq/Enumerable.cs,2b8d0f02389aab71 (or the other way around) – Patrick Hofman Nov 07 '17 at 14:16
  • 1
    @PatrickHofman That makes sense. The blog post says it was copied from what would become .NET 4: "In the version of the base class library that will ship with C# 4.0, we’ll be adding a Zip sequence operator to the standard extension methods. The code to do so is pretty trivial; if you happen to need this in C# 3.0, I’ve put the source code below. [...] Here’s the source code:" –  Nov 07 '17 at 14:32
  • There is no `Func` in .NET 3. I have tried to build a basic version [here](https://stackoverflow.com/a/47160235/284240). – Tim Schmelter Nov 07 '17 at 14:34
  • 1
    There are also no extensions in .NET 3 (by default). Apart from that, isn't this the normal implementation of `Enumerable.Zip`? http://referencesource.microsoft.com/#System.Core/System/Linq/Enumerable.cs,3ae08ae036c4ab24 – Tim Schmelter Nov 07 '17 at 15:35
  • Thank you everyone! – Saadi Nov 08 '17 at 08:55