You probably want:
SortedList.Values.Cast<Order>().FirstOrDefault(order => order.OrderID == orderID);
Of couse, if you are talking about SortedList<TKey, TValue>
, the call to Cast()
is unnecessary.
Btw, if you are poking through the Values
of a SortedList
in that manner, there is a good chance you're using a poor choice for the key / using the wrong data-structure altogether.
EDIT:
If you can't use LINQ in the project, nor do you wish to maintain separate data-structures keyed by the order's date and orderID respectively (as suggested by Ben Voigt), I see no choice but to implement the search yourself:
foreach(ShopOrder order in sortedList.Values)
{
if(order.OrderID == orderID) return order;
}
return null; // or throw an exception, whichever you find appropriate.
If you want to generalize further, write your own FirstOrDefault
implementation.