views:

132

answers:

4

Silly if you ask me. But this message is here because I will assume (probably correctly) that I am the silly one, not Microsoft. So... is there something I'm missing? Why didn't they include a "Find" method to this baby? The find could work on the Values, which are objects, so then I could do this:

someObject = SortedList.Values.Find(order => order.OrderID == orderID);
+5  A: 

You might be looking for .First(...) or .Single(...) (or their OrDefault variants) in LINQ, but that only really works well with types that implement IEnumerable<T>. A SortedList is not strongly typed, hence the Lambda won't work. Have a look at LINQ's .Cast<T>().

Daniel A. White
I use no LINQ in this project. IndexOfKey is not useful. I am looking for a certain object with a certain ID.
Axonn
A: 

Do you mean this the contains method

Woot4Moo
Hi. No ::- ). I am looking to find for a certain ID, which is member in the TValue.
Axonn
Why would ContainsValue fail this criterion?
Woot4Moo
Because my (value) object is a ShopOrder, not an int.
Axonn
@Axonn your point being? ContainsValue will let you know if it contains the object
Woot4Moo
My point being that I search for the object WHICH HAS ORDERID = X. My original code clearly shows that Contains is of no use here.
Axonn
+5  A: 

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.

Ani
My declaration is SortedList<DateTime, ShopOrder> orders = new SortedList<DateTime, ShopOrder>(comparer);The comparer is a simple DateTime comparer. I got more orders and I need to sort them by DateTime but I also want to quickly find an order ID. I think it's the right structure to use. And I do not use LINQ btw, so I see no "FirstOrDefault" ::- (.
Axonn
@ Axonn - At least in a time-complexity sense, you cannot 'quickly' find an orderID with this data-structure. Btw, Is there any reason you can't use LINQ? Are you on a a version prior to .NET 3.5?
Ani
Then you might want two collections: a `SortedList<DateTime, ShopOrder>` and a `Dictionary<int, ShopOrder>` that lets you easily lookup based on the ID.
Ben Voigt
@Axonn: Even if you can’t use the “real” LINQ, you can trivially write a `.First()` or `.FirstOrDefault()` method yourself: http://csharp.pastebin.com/zuPB2kbn
Timwi
Eventually I coded a method that sorts a normal list and that's it ::- D. Thanks for all the comments.
Axonn
Thanks Ani ::- >.
Axonn
+1  A: 

Are you searching on the same field that the list is sorted by? A binary search would be fastest, and SortedList provides the IndexOfKey function to do that.

It looks like you're searching through the values, in which case using LINQ's FirstOrDefault on the result of GetValueList should work.

See also: http://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/55241049-7e7e-4006-8e04-70779698d609

Ben Voigt
I use no LINQ in this project. IndexOfKey is not useful. I am looking for a certain object with a certain ID.
Axonn