Ayo Adesina
Ayo Adesina

Reputation: 2395

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

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?

Upvotes: 0

Views: 324

Answers (1)

Ted Nyberg
Ted Nyberg

Reputation: 7391

You should be able to just type-check it like:

var result = ancestors.SingleOrDefault(x => x is MyContentType);

Or:

var result = ancestors.OfType<MyContentType>();

Unless I'm misunderstanding your question?

Upvotes: 3

Related Questions