I am trying to split a concurentBag in 2D list of specific length.
Current code I am using. Here the bag can be of any length between 1 - 1000.
private static readonly ConcurrentBag<object> bag = new();
public static void updateResult()
{
var i = bag.GetEnumerator();
var tempL = 100;
List<List<object>> data = new();
List<object> temp = new();
while (i.MoveNext()) {
tempL -= 1;
if (tempL > 0)
{
temp.Add(i.Current);
}
else
{
data.Add(temp);
temp.Clear();
tempL = 100;
}
}
if(temp.Count > 0)
{
data.Add(temp);
}
}
Hoping to optimize the code or any inbuilt method that can replace this messy code