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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T10:34:44+00:00 2026-06-08T10:34:44+00:00

I’ve hunted around for this, and I am sure that I am just missing

  • 0

I’ve hunted around for this, and I am sure that I am just missing it because I not that good with Linq.

I have a list that looks like:

type=a, value=aaaa
type=a, value=bbbb
type=b, value=cccc
type=d, value=dddd
type=d, value=eeee
type=d, value=ffff
type=a, value=gggg
type=b, value=hhhh
type=b, value=iiii
type=b, value=jjjj

I would like to break this into sub lists, without sorting (I need the original order maintained). I would like to get back these lists, in this order in a list of lists or something similar:

List 1
type=a, value=aaaa
type=a, value=bbbb

List2
type=b, value=cccc

List 3
type=d, value=dddd
type=d, value=eeee
type=d, value=ffff

List 4
type=a, value=gggg

List 5
type=b, value=hhhh
type=b, value=iiii
type=b, value=jjjj

I would imagine that looping is not the best answer.

Any ideas much appreciated.

Long live stackoverflow.com!

Chris

Edit After Four Answers:

I checked the answers from:
* Enigmativity
* Bert Evans
* Risky Martin
* david.s

They all work nicely. Bert Evans brought up performance, which isn’t a big concern for me in this case, but I did some quick checking for the sake of the post.

I didn’t modify anyone’s code, just timed it doing 4,000 of these operations on rather short lists.

Risky’s answer was the fastest.
Bert’s answer was only a tad slower.

david’s and Enigmativity were hardly any slower, really.

I marked Risky’s answer as accepted because of performance and for pointing to the related post early on, and then coming back to provide an answer.

I would agree that Bert’s is the most readable, though.

I honestly don’t know which one I will use… actually, I will use Enigmativity’s solution because it has already taken into account that i only need the values and one key per subgroup.

  • 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-08T10:34:46+00:00Added an answer on June 8, 2026 at 10:34 am

    This could be generalized or made into an extension method, but you get the idea:

    public static IEnumerable<List<Item>> GroupConsecutive(IEnumerable<Item> items)
    {
        if (items.Any())
        {
            string firstType = items.Select(i => i.Type).First();
            var adjacents = items.TakeWhile(i => i.Type == firstType).ToList();
            yield return adjacents;
            foreach (var group in GroupConsecutive(items.Skip(adjacents.Count)))
            {
                yield return group;
            }
        }
    }
    

    Using this class:

    public class Item
    {
        public string Type { get; set; }
        public string Value { get; set; }
    }
    

    Edit: Here are the tradeoffs for this solution:

    Pros:

    • Returns a lazily evaluated collection
    • Doesn’t mutate any variables
    • Is concise

    Cons:

    • Iterates through items twice. This isn’t a big deal if items is a List, but if items is an IEnumerable that performs an expensive computation for each item, this method could be slower than other methods.

    If you want items to be iterated once with lazy evaluation, I recommend the GroupAdjacent extension method as mentioned in this answer or looping with yield return. If you want one iteration without lazy evaluation, I recommend looping or the Aggregate method.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I need a function that will clean a strings' special characters. I do NOT
I have some data like this: 1 2 3 4 5 9 2 6
I know there's a lot of other questions out there that deal with this

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.