tags:

views:

45

answers:

2

I seem to be a bit stuck on this, but my experience in Linq is not great. Basically, I have something like this code:

public class Point
{
    public int X { get; set; }
    public int Y { get; set; }
}

public class B
{
    public List<Point> Points { get; set; }
    public B(IEnumerable<int> Xs, IEnumerable<int> Ys)
    {
        // How to best combine Xs and Ys into Points ?
    }
}

Now, how do I fill in that constructor to properly join up those collections? I would normally use a .Join(), but there is no inner key to join on. It should also be able to handle the off-chance that one array has fewer elements than the other or is empty (should never occur, but it's possible).

+4  A: 

In C# 4 you can use the Zip operator:

var result = Xs.Zip( Ys, (a,b) => new Point(a,b) );

In C# 3, you can use ElementAt on one of the sequences with select to do the same thing:

var result = Xs.Select( (x,i) => new Point( x, Ys.ElementAt(i) );

The main problem with the second option is that ElementAt may be very expensive if the IEnumerable collection is itself a projection (as opposed to something that implements native indexing operations, like an array or List). You can get around that by first forcing the second collection to be a list:

var YsAsList = Ys.ToList();
var result = Xs.Select( (x,i) => new Point( x, YsAsList.ElementAt(i) );

Another issue you have to deal with (if you're not using Zip) is how to handle unbalanced collections. If one sequence is longer than the other you have to decide what the correct resolution should be. Your options are:

  1. Don't support it. Fail or abort the attempt.
  2. Zip as many items as exist in the shorter sequence.
  3. Zip as many items as exist in the longer sequence, substituting default values for the short sequence.

If you're using Zip(), you automatically end up with the second options, as the documentation indicates:

The method merges each element of the first sequence with an element that has the same index in the second sequence. If the sequences do not have the same number of elements, the method merges sequences until it reaches the end of one of them. For example, if one sequence has three elements and the other one has four, the result sequence will have only three elements.

The final step of all of this, is that you need to convert the projects result into a list to assign it to your Points object. That part is easy, use the ToList() method:

Points = Xs.Select( (x,i) => new Point( x, Ys.ElementAt(i) ).ToList();

or in C# 4:

Points = Xs.Zip( Ys, (a,b) => new Point(a,b) ).ToList();
LBushkin
Note that this is .Net 4.0 only.
Gabe
Thanks much. This seems to be exactly what I was looking for. I was under a bad assumption of what Zip would do (improperly handle unequal sizes). I am using C# 4, so this works great!
drharris
+1  A: 

Presumably it should be an error if the two collections aren't the same size or if one or both are null.

public B(IEnumerable<int> Xs, IEnumerable<int> Ys) 
{ 
    if (Xs == null || Ys == null)
       throw new ArgumentNullException( "Both collections need to be non-null" );

    if (Xs.Count() != Ys.Count())
       throw new ArgumentException( "Collections must be of the same size" );

    this.Points = Xs.Zip( (x,y) => new Point { X = x, Y = y } ).ToList();
}

If you need something for .NET 3.5, replace the last line with.

this.Points = Xs.Select( (x,i) => new Point { X = x, Y = Ys.ElementAt(i) } ).ToList();
tvanfosson
Yes, I'll be manually checking for nulls and empty. If they are not equal, I'd prefer it to take all data up to that unequal space.
drharris
@drharris -- I think Zip will do what you want. If you need it to work with 3.5, then use `Xs.Take( Ys.Count() ).Select( ... )` -- that way, if there are more Xs than Ys, you only use the first Ys-count elements from Xs. If there are more Ys than Xs, take will simply return fewer than requested elements.
tvanfosson