Search code examples
c#asp.netilistsitefinity

Storing complex data in a list


I'm working with a product called SiteFinity.

I have a class which looks like so:

public class Categories
{    
    public IContent oContent {get; set;}    
}

I'm then looping through a list and trying to check whether the current value already exists, like so:

IList items = base.CreateDataSource();
IList filteredList = new List<string>();

foreach (IContent cnt in items)
{
   if (!filteredList.Contains(cnt))
    {
        filteredList.Add(cnt);
    }
}
return filteredList;

But this returns an error. Am i using the .Contains correctly?

Update:

Ok I have updated:

List<IContent> filteredList = new List<IContent>();

However, IContent has a method that can be called to extract further information, which is like so:

foreach(IContent cnt in items)
{
    string strCat = cnt.GetMetaData("Category");
}

Now although i want filteredList to contain multiple IContent items, I want to check against the string GetMetaData before deciding whether the item should be added. Does that make sense?

Thanks.


Solution

  • You cannot add your IContent object to a List<string>, since a List<String> can only hold strings.

    Change it to a List<IContent> and it will work fine.

    Also, C#is not Java; do not declare your list variables a IList.
    Had you declared them as their actual types (List<string> and whatever CreateDataSource returns), you wouldn't have had this issue.