Try our conversational search powered by Generative AI!

How to select Items from List<IContent> based on the ContentType

Ayo
Ayo
Vote:
 

Hi guys,

I have a list of IContent Items, I know that one of the items will be of a particular type.

Currently I am getting this Item with this code:

var result = ancestors.SingleOrDefault(x => x.ContentTypeID == 104);

I know the name of MyType how can do this without an hard coded Id?

If this number is diffrent in mulitiple enviorments the code will fail, can anyone show me how to do this?

#189263
Mar 14, 2018 11:17
Vote:
 

Try this:

ancestors.OfType<MyType>().FirstOrDefault()

It should work as long as your IContent can be casted to your MyType.

#189294
Mar 14, 2018 16:42
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.