what is wrong with this code. It hangs:
public static IEnumerable<Piece> allPiecesList;
private static ConcurrentDictionary<string, Piece> allPiecesCD = new ConcurrentDictionary<string, Piece>();
using (var dc = new MyDataContext())
{
allPiecesList = dc.PopulateAllPieces("A").ToList();
Parallel.ForEach(allPiecesList , (piece) => allPiecesCD.TryAdd(piece.Name, piece));
}
Note: PopulateAllPieces uses Linq to SQL behind the covers. However, that is all done and we have a IEnumerable before the Parallel part starts.
MyDataContext is a linq to sql data context. Piece.Name is an an accessor property that does not do any computation. Basically, PopulateAllPieces returns a set of records from the database. Each of these records is a 'Piece' object. Piece.Name exposes the column called 'Name' in the database.