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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T06:01:05+00:00 2026-05-11T06:01:05+00:00

I have two or more arrays — one with IDs, one or more with

  • 0

I have two or more arrays — one with IDs, one or more with string values. I want to merge these into a hash table so I can look up values by ID.

The following function does the job, but a shorter and sweeter version (LINQ?) would be nice:

Dictionary<int, string[]> MergeArrays( IEnumerable<int> idCollection,                                        params IEnumerable<string>[] valueCollections ) {     var dict = new Dictionary<int, string[]>();      var idL = idCollection.Count();     while ( idL-- > 0 )     {         dict[idCollection.ElementAt( idL )] = new string[valueCollections.Length];          var vL = valueCollections.Length;         while ( vL-- > 0 )             dict[idCollection.ElementAt( idL )][vL] = valueCollections[vL].ElementAt( idL );     }      return dict; } 

Any ideas?

  • 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. 2026-05-11T06:01:06+00:00Added an answer on May 11, 2026 at 6:01 am

    That’s very inefficent at the moment – all those calls to ElementAt could be going through the whole sequence (as far as they need to) each time. (It depends on the implementation of the sequence.)

    However, I’m not at all sure I even understand what this code is doing (using foreach loops would almost certainly make it clearer, as would iterating forwards instead of backwards. Could you give some sample input? and expected outputs?

    EDIT: Okay, I think I see what’s going on here; you’re effectively pivoting valueCollections. I suspect you’ll want something like:

    static Dictionary<int, string[]> MergeArrays(     IEnumerable<int> idCollection,     params IEnumerable<string>[] valueCollections) {     var valueCollectionArrays = valueCollections.Select          (x => x.ToArray()).ToArray();     var indexedIds = idCollection.Select((Id, Index) => new { Index, Id });      return indexedIds.ToDictionary(x => Id,          x => valueCollectionArrays.Select(array => array[x.Index]).ToArray()); } 

    It’s pretty ugly though. If you can make idCollection an array to start with, it would frankly be easier.

    EDIT: Okay, assuming we can use arrays instead:

    static Dictionary<int, string[]> MergeArrays(     int[] idCollection,     params string[][] valueCollections) {     var ret = new Dictionary<int, string[]>();     for (int i=0; i < idCollection.Length; i++)     {          ret[idCollection[i]] = valueCollections.Select              (array => array[i]).ToArray();     }     return ret; } 

    I’ve corrected (hopefully) a bug in the first version – I was getting confused between which bit of the values was an array and which wasn’t. The second version isn’t as declarative, but I think it’s clearer, personally.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have these two pieces of code, wich one is more readable? foreach decimal
I have two arrays, one is very large (more than million entries) and other
The problem: I often have to update two or more repositories: One for the
I have two arrays of System.Data.DataRow objects which I want to compare. The rows
I have two classes, and want to include a static instance of one class
If I have two char arrays like so: char one[200]; char two[200]; And I
I have two arrays of data that I'm trying to amalgamate. One contains actual
More of a syntax curiosity than a problem to solve... I have two arrays
I have rough ideas - like dealing with singular/plural, two or more words/phrases that
I have Boys and Toys. Two django models. Toys can be owned by more

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.