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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:36:03+00:00 2026-06-14T09:36:03+00:00

I have a list of items e.g id 1 2 3 4 5 6

  • 0

I have a list of items e.g

id
1
2
3
4
5
6
7
8
9
10
11
12
13

I want to write this to csv and do it in batches of 5 so when it reaches id 5 it will go to next batch. I was thinking of doing mod to cater for this so

int noOfIds = MyIDList.Count % 5; // to get the number of loops i need

List<MyIDList> topFiveID = (from c in MyIDList
                select c).Take(5).ToList(); // get top 5 from the list

Then I am getting the rest by

List<MyIDList> restOfIDs = MyIDList.Where(c => !topFiveID.Any(tc => tc.ID == c.ID)).ToList();

Now I can see this can cater for up to 9 ids, can someone please tell me how to cater for all the ids no matter how many are there.

Hope it is clear enough.

  • 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-14T09:36:04+00:00Added an answer on June 14, 2026 at 9:36 am

    The following will return batches of a given size from an enumeration:

    public static IEnumerable<IEnumerable<TSource>> Batch<TSource>(
        this IEnumerable<TSource> source,
        int batchSize)
    {
        var batch = new List<TSource>();
        foreach (var item in source)
        {
            batch.Add(item);
            if (batch.Count == batchSize)
            {
                 yield return batch;
                 batch = new List<TSource>();
            }
        }
    
        if (batch.Any()) yield return batch;
    }
    

    Used like:

    foreach (var list in MyIdList.Batch(5))
    {
        // list is an IEnumerable<T> containing up to 5 items
        Console.WriteLine("{0}", String.Join(",", list));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a list of items: <item>a</item> <item>x</item> <item>c</item> <item>z</item> and I want as
I have a list of activity stream items where I want similar items to
I have a list which has repeating items and I want a list of
Desired Outcome I want to have Vertical List with custom items on the left/right
I have a list with numerous Items. I want to search for a term
I want to create a marketplace where users can list items and have buyers
What I want to do is add and remove list items. I have got
I have a List object and I want to remove the duplicated items but
In my viewmodel, I have a list (ObservableCollection) containing items. In the view, this
Work on C# linq.I have a list to list .From this list 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.