Setting SyndicationItem Categories property

240 Views Asked by At

I'm working with the SyndicationItem class and have found the Categories property.

According to the documentation this property doesn't have a setter, I have however found out that I can write to the property, i.e.

var syndicationItem = new SyndicationItem
{
    Categories = { new SyndicationCategory("Category name") }
};

Now, that's not good enough since I can't enumerate my values like that. If I do something like this

var categoryCollection = new Collection<SyndicationCategory>();

var syndicationItem = new SyndicationItem
{
    Categories = categoryCollection
};

The Categories property get all squiggled up and tells me there's no setter.

What am I missing?

1

There are 1 best solutions below

0
On

The documentation and the compiler are right - the Categories property doesn't have a setter.

The first example you posted does not assign a new value to the Categories property. It gets the existing value and calls the Add method to add each SyndicationCategory you specify in the initializer.

You will have to add categories to the existing collection:

var syndicationItem = new SyndicationItem();
Collection<SyndicationCategory> categoryCollection = GetCategories();
foreach (var category in categoryCollection)
{
    syndicationItem.Categories.Add(category);
}