ICollection - Get single value


Question

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

1
47
4/1/2018 8:29:15 AM

Linq, baby, yeah...

   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();
72
4/17/2015 4:09:55 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon