Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cast object type in a LINQ statement

I have a Layer collection which contains a Content object as a property. How do I cast this Content to its original object to get its property in LINQ statement?

For example:

var item = Layers.FirstOfDefault(x =>(PushPin)x.Content.Description == "xyz");

In this case Content is of PushPin object type and I want to compare its Description property to xyz

like image 520
PutraKg Avatar asked Nov 30 '25 03:11

PutraKg


1 Answers

If Content can be something other than PushPin then you will need something along the lines of

var item = Layers.FirstOrDefault(x => x.Content is PushPin && ((PushPin)x.Content).Description == "xyz");
like image 130
Rob Epstein Avatar answered Dec 02 '25 16:12

Rob Epstein



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!