my requirement is to get the list of customer and then create pdf for each of them and send into email as attachment (but the condition is to send only 10 attachment per email). So do achieve this I am querying database using Linq and getting the list of customer into a list using a WCF application. But in my case I may get any number of customer so how do I segment the total list into 10 attachment per email? Please suggest me on this. Thanks.
Asked
Active
Viewed 408 times
2 Answers
1
My code will get you going. Read up on IEnumerable and check out methods like below:
int alreadyProcessesCount = 0;
while (myList.Any())
{
var emailList = myList.Skip(alreadyProcessesCount).Take(10);
// code to attach and send here
alreadyProcessesCount += 10;
}
Take returns a specified number of contiguous elements from the start of a sequence.
Skip bypasses a specified number of elements in a sequence and then returns the remaining elements.

thewisegod
- 1,524
- 1
- 10
- 11
-
you are not taking anything away from myList so myList.Any() will always be true resulting in an infinite loop, – yohannist Sep 06 '15 at 17:00
-
This is not the full extent of code here, just something to get barsan going. Yes there will have to be some type of iterator to move through the list, but as you can see, he did not provide any code above. – thewisegod Sep 06 '15 at 17:08
1
Sometime ago i created this function to split Enumerbales in batches to process them
the usage to split a list / enumerable in parts with 10 items looks like this
foreach(var batch in mySource.Batch(10))
{
//.. Code to handle a part
}
Sourcecode:
public static IEnumerable<IEnumerable<TSource>> Batch<TSource>(this IEnumerable<TSource> source, int size)
{
TSource[] bucket = null;
int count = 0;
foreach (TSource item in source)
{
if (bucket == null)
{
bucket = new TSource[size];
}
bucket[count++] = item;
if (count != size)
{
continue;
}
yield return bucket;
bucket = null;
count = 0;
}
if (bucket != null && count > 0)
{
yield return bucket.Take(count);
}
}

Boas Enkler
- 12,264
- 16
- 69
- 143
-
How come it is exactly the same as https://stackoverflow.com/a/13731823/9921853 written 3 years before you? With exactly the same problem at `yield return bucket.Take(count);`, which I corrected in the original post. – Sergey Nudnov Nov 13 '19 at 20:10
-
can't say. especially as this post is also already 4 years old.... maybe because its just an obvious solution to make it like this? – Boas Enkler Nov 14 '19 at 08:58