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.

gunr2171
  • 16,104
  • 25
  • 61
  • 88
Joao Raposo
  • 135
  • 8

1 Answers1

4

You could use a ConcurrentDictionary<int, Order>. In particular its TryRemove method takes an ID, removes the entry, and returns the Order.

This collection is thread safe, but non blocking. As with Dictionary<TKey, TValue> keys must be unique (your use of First instead of Single suggest that constraint might be violated in your case).

CodesInChaos
  • 106,488
  • 23
  • 218
  • 262
  • That sounds a good solution for what I want. I'll probably use the order id for the key. My use of `First` on an id suggests I should actually use `Single`. Thanks for you help – Joao Raposo Mar 11 '14 at 17:33