C# – ICollection – Get single value

.netc++icollection

What is the best way to get a value from a ICollection?
We know the Collection is empty apart from that.

Best Solution

You can use LINQ for this:.

var foo = myICollection.OfType<YourType>().FirstOrDefault();
// or use a query
var bar = (from x in myICollection.OfType<YourType>() where x.SomeProperty == someValue select x)
   .FirstOrDefault();