Here is a simplified version of the code I use:
class Program
{
// Here is the ugly stuff
static void DoStuff(int[] uidArray)
{
int chunkCount = (int)Math.Ceiling(uidArray.Length / 10d);
for (int i = 0; i < chunkCount; i++)
{
// calculating the remaining uids.
// this is super ugly imho, but I couldnt come up with anything better...
int remaining = (i == chunkCount - 1) ? uidArray.Length - i * 10 : 10;
int[] currentChunks = uidArray.Skip(i * 10).Take(remaining).ToArray();
string[] data = GetDataForUids(currentChunks);
foreach (string item in data)
{
Console.WriteLine(item);
}
}
}
// Call DoStuff()
static void Main(string[] args)
{
DoStuff(Enumerable.Range(1, 21).ToArray());
Console.ReadLine();
}
// This method does is not my concern
static string[] GetDataForUids(int[] uidArray)
{
var ret = new List<string>();
foreach (int uid in uidArray)
{
ret.Add("item " + uid);
}
return ret.ToArray();
}
}
What the "real" implementation does:
- taking an
int[]
of uids - dividing them in smaller arrays, with at most 10 uids per part
- in a for loop taking part by part
- for each part call
GetDataForUids
which uses the database in order to fetch the data by uid - process the returned data in a
foreach
loop
Because of the various "outer" variables like chunkCount
I cant imagine how this thing can be written in a better way e. g. using LINQ. Is it still possible?