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

  • Home
  • SEARCH
  • 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 9050887
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T12:45:56+00:00 2026-06-16T12:45:56+00:00

I have a dictionary of type Dictionary<string, List<string>> and I want to convert it

  • 0

I have a dictionary of type Dictionary<string, List<string>> and I want to convert it to a list of type List<Dictionary<string, string>>. For e.g.

Input:

  Key              List
{ id,   { "1",     "2",    "3"   }},
{ mkt,  { "in",    "uk",   "us"  }},
{ dept, { "sales", "test", "var" }},

Output:

{ 
  { (id, "1"), (mkt , "in"), (dept, "sales") },  //1st Node as Dictionary
  { (id, "1"), (mkt , "in"), (dept, "test")  },  //2nd Node
  { (id, "1"), (mkt , "in"), (dept, "var")   },
  .
  . //All possible combinations id, mkt and dept
  .
 }

I am able to do it using for loops but I was looking for a more clean way maybe using some C# specific feature like LINQ etc.

int a = 0;
int NoOfTimesToRepeatAnElement = 1, NoOfTimesToRepeatList = count;
int prevListSize = 1, currListSize = 1;
foreach (var arg in dictionary)
{
    a = 0;
    prevListSize = currListSize;
    currListSize = arg.Value.Count();

    NoOfTimesToRepeatAnElement = NoOfTimesToRepeatAnElement * prevListSize;
    NoOfTimesToRepeatList = NoOfTimesToRepeatList / currListSize;

    var list = arg.Value;

    for (int x = 0; x < NoOfTimesToRepeatList; x++)
    {
        for (int y = 0; y < currListSize; y++)
        {
            for (int z = 0; z < NoOfTimesToRepeatAnElement; z++)
            {
                finalList[a++].Add(arg.Key, list[y]);
            }
        }
    }
}

PS: I am from C background and new to C#

  • 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-16T12:45:58+00:00Added an answer on June 16, 2026 at 12:45 pm
    var list = dict.Select(kv => kv.Value.Select(v => new { kv.Key, Value = v }))
                    .CartesianProduct()
                    .Select(x => x.ToDictionary(y=>y.Key,y=>y.Value))
                    .ToList();
    

    public static partial class MyExtensions
    {
        //http://blogs.msdn.com/b/ericlippert/archive/2010/06/28/computing-a-cartesian-product-with-linq.aspx
        public static IEnumerable<IEnumerable<T>> CartesianProduct<T>(this IEnumerable<IEnumerable<T>> sequences)
        {
            // base case: 
            IEnumerable<IEnumerable<T>> result = new[] { Enumerable.Empty<T>() };
            foreach (var sequence in sequences)
            {
                var s = sequence; // don't close over the loop variable 
                // recursive case: use SelectMany to build the new product out of the old one 
                result =
                    from seq in result
                    from item in s
                    select seq.Concat(new[] { item });
            }
            return result;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a dictionary of type Dictionary<string, IEnumerable<string>> and a list of string values.
I have the following code to populate a DropDownList var list = new Dictionary<string,
I have a function written in C# which has one parameter type as Dictionary<string
I have a python dictionary of type defaultdict(list) This dictionary is something like this:
I have dictionary, like Dictionary<string, bool> accValues = new Dictionary<string, bool>() And I want
I have a Dictionary<string, List<string>> where string is a path to a file. I
I have a function which returns a List<Dictionary<string, object>> where object is a standard
I have a class that stores a list of dictionary entries. I want bind
I am writing a dictionary-type app. I have a list of hash-mapped terms and
I have a List of Dictionaries that have keys of type string and values

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.