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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:58:01+00:00 2026-06-15T18:58:01+00:00

I have several numbered lists stored in a List<string> : List<string> all = new

  • 0

I have several numbered lists stored in a List<string>:

List<string> all = new List<string>()
{
  "1. Apple",
  "2. Banana",
  "3. Coconut",
  "1. Ant",
  "2. Beaver",
  "3. Cat"
  ...
}

I want to split this list into a list of lists where each list contains 1-3.

List<List<string>> split = new List<List<string>>()
{
  new List<string>() { "1. Apple", "2. Banana", "3. Coconut"},
  new List<string>() { "1. Ant", "2. Beaver", "3. Cat"}
}

There will ALWAYS be “1.” so I can use that as my delimiter. Is there a slick way to do this with LINQ without needing two nested for loops?

Update: I’d like this to be generalized for any length, not always 3.

  • 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-15T18:58:03+00:00Added an answer on June 15, 2026 at 6:58 pm

    Just another option to get desired result (assign group index to each item in sequence, then group by that index):

    int groupIndex = 0;
    List<List<string>> split = all.Select(s => {
                                       if (s.StartsWith("1."))
                                          groupIndex++;
                                       return new { groupIndex, s }; })
                                  .GroupBy(x => x.groupIndex)
                                  .Select(g => g.Select(x => x.s).ToList())
                                  .ToList();
    

    Another option – accumulate results (this will require one traverse over the list)

    List<List<string>> split =
        all.Aggregate(new List<List<string>>(), (acc, s) =>
                        { 
                            if (s.StartsWith("1."))
                                acc.Add(new List<string>());
                            acc[acc.Count - 1].Add(s);
                            return acc; 
                        });  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Rails application with numbered migrations 001_..., 002_..., etc. I have several
I have several cases where my string in strings.xml is quite long and has
I have several buttons. When clicked I would like to remove all classes from
We have several thousand job numbers stored in a SQL Server database, and I'd
I have several hundred products in my Magento store, all of which have a
I have several of these tags in a text document (stored in $content) with
I have several CONST's defined on some classes, and want to get a list
I have a table with several labels lbl1, lbl2...., lbl6... numbered serially I want
I have several circles on the screen, placed on different locations (all dynamic box2d
I have several dates stored as varchar in the following format: 01-01-2012 I want

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.