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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T00:19:06+00:00 2026-05-17T00:19:06+00:00

I have a List<T> that is a mixture of ‘regular’ elements and ‘marker’ elements.

  • 0

I have a List<T> that is a mixture of ‘regular’ elements and ‘marker’ elements. I want to split it into a List<List<T>>, broken at the markers.

e.g. given an input of {a, b, M, c, d, e, f, M, g}, where ‘M’ is a marker element, I want to get {{a, b},{c, d, e, f}, {g}}

It is easy to do the job with a loop, but it seems like it should be possible to express it more compactly with LINQ, and after a bit of head-scratching I just can’t see how. (Not enough LINQ-fu, I guess.)

  • 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-17T00:19:07+00:00Added an answer on May 17, 2026 at 12:19 am

    Hmm…

    varSplitList = myList.Aggregate(new List<List<T>>{new List<T>()}, 
       (sl,t)=> {
          if(/*T is a marker element*/) sl.Add(new List<T>());
          else sl.Last().Add(t); 
          return sl;
       });
    

    There’s probably a more readable way, but that should work. Aggregate is a very powerful “series calculation” method that is useful for these kinds of things. You provide it a “seed” (in this case a new List of Lists with a single child List), and for each element in the source, perform the operation, which takes the seed and the current element and returns the (probably modified) seed, which is then passed to the operation for the next element.

    As a simpler example, here’s a Factorial calculator in Linq:

    long fact = Enumerable.Range(1,n).Aggregate(1, (f,n)=>f*n));

    Enumerable.Range() produces an integer range from 1 to n. the Aggregate function starts with 1, and for each element, multiplies the seed by the element:

    1*1=1
    1*2=2
    2*3=6
    6*4=24
    24*5=120…

    At the end, fact is given the value of the seed after all calculations are performed.

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

Sidebar

Related Questions

I have a linked list that I want to sort part of, eg: std::sort(someIterator,
I have a list that looks like (A (B (C D)) (E (F))) which
I have a specialized list that holds items of type IThing : public class
I have a dropdown list that stores name/value pairs. The dropdown appears in each
I have a list wrapper that maintains two Tstringlists and a TClassList I need
I have a list view that is periodically updated (every 60 seconds). It was
i have a list of regex patterns (stored in a list type) that I
Let's say I have a List object and an iterator for that list. Now
i have a list of products that are being displayed in particular order. store
We have an auto-complete list that's populated when an you send an email to

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.