Geeks With Blogs
Works on my machine

I found that Linq’s ZIP is really great for adjacent item computations.

For example, let’s have a collection of dates:

var dates = new DateTime[]
{
    new DateTime(2000,1,1),
    new DateTime(2000,1,2),
    new DateTime(2000,1,5)
};

How would you compute time difference of adjacent items ?

I like to use Zip for this kind of job:

dates.Zip(dates.Skip(1), (d1, d2) => d2 - d1);

As you might expect, the result will be: { 1 day, 3 days }

Posted on Tuesday, October 30, 2012 9:52 PM C# , LINQ | Back to top


Comments on this post: Linq’s ZIP for adjacent items

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © Jakub Niemyjski | Powered by: GeeksWithBlogs.net | Join free