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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T22:30:45+00:00 2026-05-12T22:30:45+00:00

I have a generic List (of Foo) which contains n objects of Type Foo.

  • 0

I have a generic List (of Foo) which contains n objects of Type Foo. One of the properties of Foo is PropertyA. PropertyA can be one of ValueA, ValueB or ValueC. Is there an easy way of splitting this into three seperate Lists, one for ValueA, one for ValueB and one for ValueC?

I can write some code which loops the original list and adds each item to a new list based on the property value but this does not seem to be very maintainable (what if I suddenly get a ValueD for example?)

**EDIT. I should have mentioned that I’m using version 2.0 of the framework.

  • 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-12T22:30:45+00:00Added an answer on May 12, 2026 at 10:30 pm

    In C# with .Net 2.0, I have written (too many times):

     //if PropertyA is not int, change int to whatever that type is
    Dictionary<int, List<foo>> myCollections =
      new Dictionary<int, List<foo>>();
    //
    foreach(Foo myFoo in fooList)
    {
      //if I haven't seen this key before, make a new entry
      if (!myCollections.ContainsKey(myFoo.PropertyA))
      {
        myCollections.Add(myFoo.PropertyA, new List<foo>());
      }
      //now add the value to the entry.
      myCollections[myFoo.PropertyA].Add(myFoo);
    }
    //
    // now recollect these lists into the result.
    List<List<Foo>> result = new List<List<Foo>>();
    foreach(List<Foo> someFoos in myCollections.Values)
    {
      result.Add(someFoos);
    }
    

    Nowadays, I just write:

    List<List<foo>> result = fooList
      .GroupBy(foo => foo.PropertyA)
      .Select(g => g.ToList())
      .ToList();
    

    Or

     ILookup<TypeOfPropertyA, foo>> result = fooList.ToLookup(foo => foo.PropertyA);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have generic list which must be a preserved order so I can retrieve
I have a linked list of Foo objects. Foo is a base class, which
I have a generic List<MyClass> where MyClass has a property InvoiceNumber which contains values
I have a Generic list of Objects. Each object has 9 string properties. I
I have a generic list of objects in C#, and wish to clone the
I have a generic list of objects in C#, for example sake, here's what
Can anyone help.. I have a generic list like so IList<itemTp> itemTps; itemTp basically
I have an abstract class foo that contains some properties and methods. I have
I have a generic list which I form from two other lists. The orderby
I want to create a generic list of the Type object. I have ...

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.