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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T04:24:47+00:00 2026-06-04T04:24:47+00:00

This is probably a simple question, but the answer is eluding me. I have

  • 0

This is probably a simple question, but the answer is eluding me.

I have a collection of strings that I’m trying to convert to a dictionary.

Each string in the collection is a comma-separated list of values that I obtained from a regex match. I would like the key for each entry in the dictionary to be the fourth element in the comma-separated list, and the corresponding value to be the second element in the comma-separated list.

When I attempt a direct call to ToDictionary, I end up in some kind of loop that appears to kick me of the BackgroundWorker thread I’m in:

var MoveFromItems = matches.Cast<Match>()
                           .SelectMany(m => m.Groups["args"].Captures
                           .Cast<Capture>().Select(c => c.Value));

var dictionary1 = MoveFromItems.ToDictionary(s => s.Split(',')[3], 
                                             s => s.Split(',')[1]);

When I create the dictionary manually, everything works fine:

var MoveFroms = new Dictionary<string, string>();

foreach(string sItem in MoveFromItems) 
{
    string sKey = sItem.Split(',')[3]; 
    string sVal = sItem.Split(',')[1];

    if(!MoveFroms.ContainsKey(sKey))
        MoveFroms[sKey.ToUpper()] = sVal;
}

I appreciate any help you might be able to provide.

  • 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-04T04:24:48+00:00Added an answer on June 4, 2026 at 4:24 am

    The problem is most likely that the keys have duplicates. You have three options.

    Keep First Entry (This is what you’re currently doing in the foreach loop)

    Keys only have one entry, the first one that shows up – meaning you can have a Dictionary:

    var first = MoveFromItems.Select(x => x.Split(','))
                             .GroupBy(x => x[3])
                             .ToDictionary(x => x.Key, x => x.First()[1]);
    

    Keep All Entries, Grouped

    Keys will have more than one entry (each key returns an Enumerable), and you use a Lookup instead of a Dictionary:

    var lookup = MoveFromItems.Select(x => x.Split(','))
                              .ToLookup(x => x[3], x => x[1]);
    

    Keep All Entries, Flattened

    No such thing as a key, simply a flattened list of entries:

    var flat = MoveFromItems.Select(x => x.Split(','))
                            .Select(x => new KeyValuePair<string,string>(x[3], x[1]));
    

    You could also use a tuple here (Tuple.Create(x[3], x[1]);) instead.


    Note: You will need to decide where/if you want the keys to be upper or lower case in these cases. I haven’t done anything related to that yet. If you want to store the key as upper, just change x[3] to x[3].ToUpper() in everything above.

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

Sidebar

Related Questions

This is probably a question that has an easy/simple/obvious answer, but I've found myself
There is probably a simple answer to this question but I do not have
This is probably a really dumb question with a simple answer but... I am
This is probably a simple question, but i have been unable to find a
this is probably too simple of a question, but here I go. I have
This is probably a basic html/css question... I have a simple one-button form that
I feel like the answer to this question is probably very simple, but I'm
I've got a question that probably has a pretty simple answer, but I didn't
There's probably a Google search that'll answer this question but for the life of
All, This question probably has a very simple answer - something I'm overlooking. But

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.