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 8096897
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T21:35:15+00:00 2026-06-05T21:35:15+00:00

I have a list of ProjectItem class instances: List<ProjectItem> allProjects . I need to

  • 0

I have a list of ProjectItem class instances: List<ProjectItem> allProjects.

I need to obtain a List<ProjectItemPair> – a list of new class instances, that have ProjectItem projectOne and ProjectItem projectTwo as a properties.

I had to foreach the initial list, and using a bool switch was adding a new ProjectItemPair into a resulting list on every second item in iteration. This is a primitive old school approach. I am sure with Linq I can pair the project instances up effectively. How can I do it?

  • 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-06-05T21:35:16+00:00Added an answer on June 5, 2026 at 9:35 pm

    Well, you could use:

    var pairs = allProjects.Select((value, index) = > new { value, index })
                           .GroupBy(x => x.index / 2, x => x.value)
                           .Select(pair => new ProjectItemPair(pair.ElementAt(0),
                                                         pair.ElementAtOrDefault(1)))
                           .ToList();
    

    The use of ElementAtOrDefault means the constructor will be passed null if there’s an odd number of elements.

    Another somewhat simpler alternative:

    var pairs = Enumerable.Range(0, allProjects.Count / 2)
                          .Select(i => new ProjectItemPair(allProjects[i * 2],
                                                           allProjects[i * 2 + 1]))
                          .ToList();
    

    This will omit the final element if the input has an odd number of elements, in which case you can do:

    if ((allProjects.Count % 2) == 1)
    {
        pairs.Add(new ProjectItemPair(allProjects.Last(), null);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have list of items that need to be processed and printed to the
i have list of rows that user select and i want to delete them,
I have List like this List<string> items = new List<string>(); items.Add(-); items.Add(.); items.Add(a-); items.Add(a.);
I have List collection that is populated in specific order (the requirement is that,
I have List<Product[]> and I need to join them into one Product[] .
i have list of items IList with data that looks list this: GenId TestMode
I have list of Guid's List<Guid> MyList; I need to copy its contents to
I have list of rows that I have fetched using mysql_fetch_array() . Now I
I have List<Custom> where Custom is class Custom { public int id; public String
I have list : List<string> str = new List<string>(); str.Add(asdf---US,IN); str.Add(asdg---UK,IN); str.Add(asjk---RU,IN); str.Add(asrt---IT,DE); I

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.