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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:45:38+00:00 2026-05-23T02:45:38+00:00

The following code shows what I want to do: public static IEnumerable<IEnumerable<T>> DoIt<T>(this IEnumerable<T>

  • 0

The following code shows what I want to do:

public static IEnumerable<IEnumerable<T>> DoIt<T>(this IEnumerable<T> that)
{
    if (that == null)
        throw new ArgumentNullException();

    if (that.Count() > 1)
    {
        var result = new Collection<IEnumerable<T>>();
        var collection = new Collection<T>();

        collection.Add(that.ElementAt(0));
        for (int i = 1; i < that.Count(); ++i)
        {
            if (!that.ElementAt(i).Equals(that.ElementAt(i - 1)))
            {
                result.Add(collection);
                collection = new Collection<T>();
            }

            collection.Add(that.ElementAt(i));
        }

        result.Add(collection);
        return result;
    }

    return new Collection<IEnumerable<T>>() { that };
}

I’m only using custom implementations like that one, if there is no appropriate implementation already existing. Is there any way to do the same with the standard 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-23T02:45:39+00:00Added an answer on May 23, 2026 at 2:45 am

    There is no traditional way to do this with the standard framework. I do have a couple of issues with your solution though.

    1. The use of ElementAt(i) is very inefficient and can cause the that collection to be iterated many, many times. This can lead to performance issues
    2. The use of Count also can be costly as it can cause a full enumeration of that
    3. Unlike most LINQ methods it doesn’t use deferred execution. To fix this you will need to use a yield return style solution.

    Here’s an alternative solution

    public static IEnumerable<IEnumerable<T>> DoIt<T>(this IEnumerable<T> that) {
      using (var e = that.GetEnumerator()) {
        if (!e.MoveNext()) {
          yield break;
        }
    
        bool hasMore;
        do {
          var item = e.Current;
          var list = new List<T>();
          list.Add(item);
    
          hasMore = e.MoveNext();
          while (hasMore && item.Equals(e.Current)) {
            list.Add(e.Current);
            hasMore = e.MoveNext();
          }
    
          yield return list;
        } while (hasMore);
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

** New EDIT ** so what I'm trying to do is this. I want
I am a beginner in C# and I want to do the following: Thread
I want to make a C# Dictionary in which the key is the string
SHORT FORM(!) In response the Jon Skeet's comment, what I want C# to be
OK, so this is leading on from another question I asked here recently. Basically,
<!DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0 Transitional//EN http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd> <html xmlns=http://www.w3.org/1999/xhtml dir=ltr> <head> <script src=http://ajax.googleapis.com/ajax/libs/jquery/1.4.0/jquery.min.js
I am currently investigating a performance issue in an application and have highlighted the
I have read the GtkSharp TreeView tutorial wherein the author describes how to setup
I have a WordPress website with a custom theme. I'm using custom author template
I'm starting learning android development, so my knowledge is really limited at the moment.

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.