Im having a problem here. im trying to use Parallel.foreach to convert my datatable to a list object. like this .
public List<ProductList> GetProductList(DataTable table)
{
List<ProductList> list = new List<ProductList>();
Parallel.ForEach(table.AsEnumerable(), item =>
{
string sku = item["product_sku"].ToString();
//int skus = Convert.ToInt16(item["product_sku"]);
string price = item["product_price"].ToString();
string tweakerID = item["ID"].ToString();
string finalPrice = item["FinalPrice"].ToString();
list.Add(new ProductList() { SKU = sku, Price = price, ID = id, FinalPrice = finalPrice });
});
list.RemoveAll(item => item == null);
return list;
}
i have over 65000 product rows. and after this . there are only about 63000 products added in to the list. but the result is not a fix number. for example last three times that i ran this code i got 63202 , 64025 , 62920 . and everytime its a new number.
i also get no exception .
Thats because
List<T>
is not concurent safe. Try that:ConcurrentBag<T>
instead.ConcurentBag
exists inSystem.Collections.Concurrent
namespace, which contains few more thread safe collections.All items are processed but not all are added to List. Reason for that is, when two threads try to add different items on exactly same time, list is not able to deal with it, and saves only one of them.