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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:11:06+00:00 2026-06-12T15:11:06+00:00

Is there a more elegant way to implement going 5 items at a time

  • 0

Is there a more elegant way to implement going 5 items at a time than a for loop like this?

var q = Campaign_stats.OrderByDescending(c=>c.Leads).Select(c=>c.PID).Take(23);
var count = q.Count();
for (int i = 0; i < (count/5)+1; i++)
{
   q.Skip(i*5).Take(5).Dump();
}
  • 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-12T15:11:08+00:00Added an answer on June 12, 2026 at 3:11 pm

    So you want to efficiently call Dump() on every 5 items in q.

    The solution you have now will re-iterate the IEnumerable<T> every time through the for loop. It may be more efficient to do something like this: (I don’t know what your type is so I’m using T)

    const int N = 5;
    T[] ar = new T[N];               // Temporary array of N items.
    int i=0;
    foreach(var item in q) {         // Just one iterator.
        ar[i++] = item;              // Store a reference to this item.
        if (i == N) {                // When we have N items,
            ar.Dump();               // dump them,
            i = 0;                   // and reset the array index.
        }
    }
    
    // Dump the remaining items
    if (i > 0) {
        ar.Take(i).Dump();
    }
    

    This only uses one iterator. Considering your variable is named q, I’m assuming that is short for “query”, which implies this is against a database. So using just one iterator may be very beneficial.


    I may keep this code, and wrap it up in an extension method. How about “clump”?

    public static IEnumerable<IEnumerable<T>> Clump<T>(this IEnumerable<T> items, int clumpSize) { 
        T[] ar = new T[clumpSize];
        int i=0;
        foreach(var item in items) {
            ar[i++] = item;
            if (i == clumpSize) {
                yield return ar;
                i = 0;
            }
        }
        if (i > 0)
            yield return ar.Take(i);
    }
    

    Calling it in the context of your code:

    foreach (var clump in q.Clump(5)) {
        clump.Dump();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm wondering if there is a more elegant way to do this: $foo =
I would like to ask if there is a more elegant way to write
Is there more elegant (less code) way of find a matrix OUT, with colSums(OUT)<=a
I was wondering if there is a more elegant way to do IN() queries
I'm a beginner in Flex so there must be more elegant way of doing
Is there an elegant way to make whitespace string. To be more precise, I'm
Is there a more efficent way of doing this in terms of memory usage
Is there a more elegant way to do what I'm doing below? That is,
Is there a more elegant solution to convert an ' Arraylist ' into a
There are more than one solution for finding the-number-of-digits in a given number. For

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.