0

I'm using a blocking collection as I need this list to be thread safe:

Orders = new BlockingCollection<Order>();

I'm trying to remove a specific order, lets say I want to remove order.ID 1

if it was a normal collection would be something like

orders.Remove(orders.Where(o => o.ID == 1).First());   

I've read about those collections there is Take() and TryTake() but none of them allows me to specify which one I want to remove.

4

1 回答 1

4

你可以使用一个ConcurrentDictionary<int, Order>. 特别是它的TryRemove方法接受一个 ID,删除条目,然后返回Order.

这个集合是线程安全的,但非阻塞的。与Dictionary<TKey, TValue>键必须是唯一的一样(在您的情况下,您使用First而不是Single建议可能违反约束)。

于 2014-03-11T17:26:49.157 回答