Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 887909
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T13:18:22+00:00 2026-05-15T13:18:22+00:00

I seem to be a bit stuck on this, but my experience in Linq

  • 0

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).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T13:18:22+00:00Added an answer on May 15, 2026 at 1:18 pm

    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();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on learning Objective-C/Coaoa, but I've seem to have gotten a bit stuck
This question may seem a little bit stackoverflow-implementation specific, but I have seen a
This may seem a bit crazy, but if you can tell me a better
The question might seem a bit confusing but I have an if statement that
This might seem a bit weird, but since I am new in RoR and
Im trying to accomplish something which may seem a bit convoluted but would be
I have a bit of a problem that I can't seem to code my
I'm having a great time learning Python, but I've just gotten a bit stuck
I am a bit stuck with this. I have to interface with an api
I think my question might seem a bit odd, but here it goes; I'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.