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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:50:00+00:00 2026-05-26T10:50:00+00:00

Not to be confused with: How can I convert List to Hashtable in C#?

  • 0

Not to be confused with: How can I convert List to Hashtable in C#?

I have a list of Models that I want to organize into a hashtable with the enums as the key, and the list of Models (that have the enum’s value) as the value.

public enum MyEnum
{
    Blue,
    Green
}

public class MyModel
{
    public MyModel(MyEnum myEnum, string foo, int bar)
    {
        this.MyEnum = myEnum;
        this.Foo = foo;
        this.Bar = bar;
    }

    public MyEnum MyEnum { get; set; }
    public string Foo { get; set; }
    public int Bar { get; set; }
}

List<MyModel> models = new List<MyModel>();

models.Add(new MyModel(MyEnum.Blue, "How", 1));
models.Add(new MyModel(MyEnum.Green, "Now", 2));
models.Add(new MyModel(MyEnum.Blue, "Brown", 3));
models.Add(new MyModel(MyEnum.Green, "Cow", 4));

What is the easiest way to convert the list into a hash table that would look like this (Forgive the json):

{ 
   Blue:
   [
      { Blue, "How", 1 },
      { Blue, "Brown", 3 }
   ],
   Green:
   [
      { Green, "Now", 2 },
      { Green, "Cow", 4 }
   ]
}

When I do something like this:

var taskTable = tasks.Cast<TaskModel>().ToDictionary(o => o.Task); (Where Task is the Enum)

I get this exception: “An item with the same key has already been added.”

One liners with Lambda or Linq are appreciated!

  • 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-26T10:50:01+00:00Added an answer on May 26, 2026 at 10:50 am

    It looks to me like you could do with a Lookup:

    var lookup = models.ToLookup(o => o.Task);
    

    Or if you really want the Dictionary<MyEnum, List<TaskModel>>:

    var dictionary = models.GroupBy(o => o.Task)
                           .ToDictionary(g => g.Key, g => g.ToList());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am confused. I can not use this on a float? Must it be
Hi I have problem that I can't solve alone since damn debugging doesn't work
(Not to be confused with Xunit , a popular .Net unit testing library.) Today
Does the Factory Method pattern (not to be confused with the Factory or Abstract
I am interested in parsing regexes (not to be confused with using regexes for
I am confused as to why the following code snippet would not exit when
I'm a bit confused whether or not I should make the links on my
I'm not sure how to word this cause I am a little confused at
I'm a bit confused about inheritance under sqlalchemy, to the point where I'm not
I am not sure I worded that correctly but python and time always confuses

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.