-2

I have this problem where I got a list of 5000 and 2000 entries.. The thing is everyone I want to use the 5000 entries list I have to manually type:

IEnumerable<string> partList1;
IEnumerable<string> partList2;
IEnumerable<string> partList3;
IEnumerable<string> partList4;

And then I go with:

partList1 = SplitList(IDList, 0, 999); //args = list,skipvalue,takevalue

And I have to do this for all 4 lists and if I use the 2000 entries list I only need two partLists. How can I go for it to make it dynamiclly that how often 999 fits into the list it creates the partLists itself?

I tried something like:

for(int i = 0; i < IDList.Count() / 999; i++) {
//and here I stuck
}
darby
  • 127
  • 8
  • 2
    I don't really understand the question here. Could you please elaborate bit clear. – Thangadurai Nov 25 '19 at 08:26
  • @Thangadurai I am trying to create a IEnumerable dynamiclly for the amount of how much 999 fits into a given number like when the number is 2000 I can create 2 IEnumerable and if the number is like 5000 I am able to create 5 IEnumerable // I don't really know if this makes sense :/ or if am too dumb to get this done – darby Nov 25 '19 at 08:29
  • 1
    @darby, One easy way to achieve it would be to make the ```partlist``` itself a list i.e ```IEnumerable> partlist``` and fill it as required. – gvmani Nov 25 '19 at 08:31
  • So you want to split one list into several smaller once of specific size? – Magnus Nov 25 '19 at 08:31
  • @darby can you please edit your question and clarify further your question ? It's not clear enough. – Fourat Nov 25 '19 at 08:32
  • I don't really understand – Marco Salerno Nov 25 '19 at 08:36
  • @gvmani This would be an perfect solution.. Holy heck thank you :) – darby Nov 25 '19 at 08:57
  • Related: https://stackoverflow.com/questions/11463734/split-a-list-into-smaller-lists-of-n-size, many of the answer there return inemerable – xdtTransform Nov 25 '19 at 08:57

1 Answers1

1

If I understand your problem correctly, you're trying to split some list items into chunks of 1000 (or 999; that's not clear) items and create an IEnumerable<string> for each chunk. If so, you may create a List<IEnumerable<string>> to dynamically create your lists and use the .Skip() and .Take() link methods to "split" the main list.

Try something like this:

var listOfLists = new List<IEnumerable<string>>();
const int chunkSize = 1000;

int consumed = 0;
while (consumed < IDList.Count)
{
    listOfLists.Add(IDList.Skip(consumed).Take(chunkSize).ToList());
    consumed += chunkSize;
}
  • There will be multiple enumerations of the IDlist in the "while" loop, better to convert it to array before the loop, `var idList = IDList as string[] ?? IDList.ToArray();` instead of converting each chunk with `.ToList()` – Alex B. Nov 25 '19 at 08:48