How do I work with collections in c#?

Hi,

Using c# I have built a collection (ImageLinkCollection) of ImageLinkItems (attached). I now need to attach this collection to a specific category (Brand) Id but I'm not too sure how to do this, or even if I am going about it the correct way? Can I some how add a collection property to a class? Unfortunately the below code only allows one singular ImageLinkItem to be attached to a specific BrandCategoryId, I require a collection of ImageLinkItems to be attached somehow?

Thanks.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using Microsoft.SharePoint;

namespace UniTech.Heineken.Phoenix.Service.Models
{
    [DataContract]
    public class Brand : ListItem
    {
        [DataMember]
        public int BrandCategoryId { get; set; }
        [DataMember]
        public ImageDownload Graph { get; set; }
        [DataMember]
        public string Intro { get; set; }
        [DataMember]
        public ImageDownload Logo { get; set; }
        [DataMember]
        public ImageDownload BannerAd { get; set; }
        [DataMember]
        public int OutletFit1Id { get; set; }
        [DataMember]
        public int OutletFit2Id { get; set; }
        [DataMember]
        public int OutletFit3Id { get; set; }
        [DataMember]
        public string Quote { get; set; }
        [DataMember]
        public string Colour1 { get; set; }
        [DataMember]
        public string Colour2 { get; set; }
        [DataMember]
        public string FontColour { get; set; }
        [DataMember]
        public ImageLinkItem OnTheInternet1 { get; set; }

 
        public Brand()
            : base()
        {
        }

        public Brand(int id, string title, string path, DateTime created, DateTime modified)
            : base(id, title, path, created, modified)
         {

         }

   

    }

    [CollectionDataContract]
    public class BrandCollection : List<Brand>
    {
        public void Add(SPBrand spbrand)
        {
            Brand b = new Brand(Converters.ConvertNullableInt(spbrand.Id), spbrand.Title, spbrand.Path,
                Converters.ConvertNullableDate(spbrand.Created), Converters.ConvertNullableDate(spbrand.Modified));

            SPFieldUrlValue spLogo = spbrand.Logo as SPFieldUrlValue;
            if(spLogo != null)
                b.Logo = new ImageDownload(b.Id, spLogo.Description, spLogo.Url, b.Created, b.Modified);

            SPFieldUrlValue spGraph = spbrand.Graph as SPFieldUrlValue;
            if (spGraph != null)
                b.Graph = new ImageDownload(b.Id, spGraph.Description, spGraph.Url, b.Created, b.Modified);
                       
            SPFieldUrlValue spBannerAd = spbrand.BannerAd as SPFieldUrlValue;
            if (spBannerAd != null)
                b.BannerAd = new ImageDownload(b.Id, spBannerAd.Description, spBannerAd.Url, b.Created, b.Modified);

            b.Colour1 = spbrand.Colour1;
            b.Colour2 = spbrand.Colour2;
            b.FontColour = spbrand.FontColour;

            b.Quote = spbrand.Quote;
            b.Intro = spbrand.IntroParagraph;

            if (spbrand.PriorityOutletFit1 != null)
                b.OutletFit1Id = spbrand.PriorityOutletFit1.Id.GetValueOrDefault(-1);
            if(spbrand.PriorityOutletFit2 != null)
                b.OutletFit2Id = spbrand.PriorityOutletFit2.Id.GetValueOrDefault(-1);
            if(spbrand.PriorityOutletFit3 != null)
                b.OutletFit3Id = spbrand.PriorityOutletFit3.Id.GetValueOrDefault(-1);

            if (spbrand.BrandCategory != null)
                b.BrandCategoryId = Converters.ConvertNullableInt(spbrand.BrandCategory.Id);  

            if(spbrand.ImageLink != null)
            {
                var orderedLinks = spbrand.ImageLink.OrderByDescending(l => l.Published);

                foreach(ImageLink link in orderedLinks)
                {
                    string strUrl = link.URL.IndexOf(',') > 0 ? link.URL.Split(',')[0].Trim() : "";
                    string strTitle = link.URL.IndexOf(',') > 0 ? link.URL.Split(',')[1].Trim() : "";
                    b.OnTheInternet1 = new ImageLinkItem(link.Id, strTitle, link.Path, DateTime.MinValue, DateTime.MinValue);
                    b.OnTheInternet1.Url = strUrl;
                    b.OnTheInternet1.Order = (int) Converters.ConvertNullableDouble(link.Order);
                    b.OnTheInternet1.Logo = ???????
                }
            }

            Add(b);
        }

        public void Add(List<SPBrand> spbrands)
        {
            foreach (SPBrand spbrand in spbrands)
            {
                Add(spbrand);
            }
        }
       
    }
}






using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Policy;
using System.Text;
using System.Runtime.Serialization;
using Microsoft.SharePoint;

namespace UniTech.Heineken.Phoenix.Service.Models
{
    [DataContract]
    public class ImageLinkItem : ListItem
    {
        [DataMember]
        public ImageDownload Logo { get; set; }
        [DataMember]
        public string Url { get; set; }
        [DataMember]
        public int Order { get; set; }

        public ImageLinkItem()
        {

        }

        public ImageLinkItem(int? id, string title, string path, DateTime created, DateTime modified)
            : base(id, title, path, created, modified)
        {
            
        }
    }

    //public class ImageLinkCollection : List<ImageLinkItem>
    //{
        
    //}

    [CollectionDataContract]
    public class ImageLinkCollection : List<ImageLinkItem>
    {
        public void Add(SPBrandImageLink spImageLink)
        {
            string strUrl = spImageLink.URL.IndexOf(',') > 0 ? spImageLink.URL.Split(',')[0].Trim() : "";
            string strTitle = spImageLink.URL.IndexOf(',') > 0 ? spImageLink.URL.Split(',')[1].Trim() : "";
            ImageLinkItem i = new ImageLinkItem(Converters.ConvertNullableInt(spImageLink.Id), strTitle,
                                                spImageLink.Path,
                                                Converters.ConvertNullableDate(spImageLink.Created),
                                                Converters.ConvertNullableDate(spImageLink.Modified));

            SPFieldUrlValue spLogo = spImageLink.Logo as SPFieldUrlValue;
            if (spLogo != null)
                i.Logo = new ImageDownload(i.Id, spLogo.Description, spLogo.Url, i.Created, i.Modified);
            i.Url = strUrl;
            i.Order = (int)Converters.ConvertNullableDouble(spImageLink.Order);

            Add(i);
        }

        public void Add(List<SPBrandImageLink> spImageLinks)
        {
            foreach (SPBrandImageLink spImageLink in spImageLinks)
            {
                Add(spImageLink);
            }
        }
    }
}

Open in new window

aspnet-scotlandAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
guru_samiConnect With a Mentor Commented:
I see...remove line 6 and modify line 5 like below:

ImageLinkItem ili = new ImageLinkItem(link.Id, strTitle, link.Path, DateTime.MinValue, DateTime.MinValue);
0
 
guru_samiCommented:
I am not sure if I am understanding it right but a property like this you mean?

private ImageLinkCollection  _ImageLinkList ;
public ImageLinkCollection  ImageLinkList
        {
            get
            {
                if (_ImageLinkList == null)
                {
                    _ImageLinkList = new ImageLinkCollection ();
                }
                return _ImageLinkList ;
            }
            set { _ImageLinkList = value; }
        }

I am using something similar.
0
 
aspnet-scotlandAuthor Commented:
Thanks for your reply. I'm not too sure if your solution works or not yet as I am not at my dev machine but I thought I would offer you more of an explanation:

Within the code that I posted I have the property "OnTheInternet1" which is of type ImageLinkItem, this allowed me to set only one set of values (OnTheInternet1.Id, OnTheInternet1.Logo, OnTheInternet1.URL etc) per a BrandCategoryId, this unfortunately did not meet my requirements as many OnTheInternet items are required to be mapped against any one BrandCategoryId, thus the reason behind building the ImageLinkCollection. Basically if you could imagine a brand like Nike, what I'm wanting to do is add not just one but many image links (to external web sites) to this brand, say to trainers and golf clubs. Will your code allow me to start the possibility of iterating through my collection returning only the ImageLinkItems for a specific brand?

Thanks.
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
guru_samiCommented:
I think then what I said should work.
In your loop:
foreach(ImageLink link in orderedLinks)
{
    //Create ImageLinkItem object and set the properties
    //Add this item to the collection ImageLinkList property as defined in my previous comment
}


0
 
aspnet-scotlandAuthor Commented:
Making my loop read:

foreach(ImageLink link in orderedLinks)
                {
                    string strUrl = link.URL.IndexOf(',') > 0 ? link.URL.Split(',')[0].Trim() : "";
                    string strTitle = link.URL.IndexOf(',') > 0 ? link.URL.Split(',')[1].Trim() : "";
                    ImageLinkItem ili = b.ImageLinkList;
                    ili = new ImageLinkItem(link.Id, strTitle, link.Path, DateTime.MinValue, DateTime.MinValue);
                    ili.Url = strUrl;
                    ili.Order = (int)Converters.ConvertNullableDouble(link.Order);
                    SPFieldUrlValue spOnTheInternetLogo = link.Logo as SPFieldUrlValue;
                    if (spOnTheInternetLogo != null)
                        ili.Logo = new ImageDownload(ili.Id, spOnTheInternetLogo.Description, spOnTheInternetLogo.Url, ili.Created, ili.Modified);
                   
                }

Unfortunately I receive the error "Cannot implicitly convert type 'ImageLinkCollection' to 'ImageLinkItem' on line "ImageLinkItem ili = b.ImageLinkList;"?

Full page code attached and I have left the ImageLinkCollection code the same.

Thanks.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using Microsoft.SharePoint;

namespace UniTech.Heineken.Phoenix.Service.Models
{
    [DataContract]
    public class Brand : ListItem
    {
        [DataMember]
        public int BrandCategoryId { get; set; }
        [DataMember]
        public ImageDownload Graph { get; set; }
        [DataMember]
        public string Intro { get; set; }
        [DataMember]
        public ImageDownload Logo { get; set; }
        [DataMember]
        public ImageDownload BannerAd { get; set; }
        [DataMember]
        public int OutletFit1Id { get; set; }
        [DataMember]
        public int OutletFit2Id { get; set; }
        [DataMember]
        public int OutletFit3Id { get; set; }
        [DataMember]
        public string Quote { get; set; }
        [DataMember]
        public string Colour1 { get; set; }
        [DataMember]
        public string Colour2 { get; set; }
        [DataMember]
        public string FontColour { get; set; }
        [DataMember]
        private ImageLinkCollection _ImageLinkList;
        public ImageLinkCollection ImageLinkList
        {
            get
            {
                if (_ImageLinkList == null)
                {
                    _ImageLinkList = new ImageLinkCollection();
                }
                return _ImageLinkList;
            }
            set { _ImageLinkList = value; }
        }

        public Brand()
            : base()
        {
        }

        public Brand(int id, string title, string path, DateTime created, DateTime modified)
            : base(id, title, path, created, modified)
         {

         }

   

    }

    [CollectionDataContract]
    public class BrandCollection : List<Brand>
    {
        public void Add(SPBrand spbrand)
        {
            Brand b = new Brand(Converters.ConvertNullableInt(spbrand.Id), spbrand.Title, spbrand.Path, 
                Converters.ConvertNullableDate(spbrand.Created), Converters.ConvertNullableDate(spbrand.Modified));

            SPFieldUrlValue spLogo = spbrand.Logo as SPFieldUrlValue;
            if(spLogo != null)
                b.Logo = new ImageDownload(b.Id, spLogo.Description, spLogo.Url, b.Created, b.Modified);

            SPFieldUrlValue spGraph = spbrand.Graph as SPFieldUrlValue;
            if (spGraph != null)
                b.Graph = new ImageDownload(b.Id, spGraph.Description, spGraph.Url, b.Created, b.Modified);
                        
            SPFieldUrlValue spBannerAd = spbrand.BannerAd as SPFieldUrlValue;
            if (spBannerAd != null)
                b.BannerAd = new ImageDownload(b.Id, spBannerAd.Description, spBannerAd.Url, b.Created, b.Modified);

            b.Colour1 = spbrand.Colour1;
            b.Colour2 = spbrand.Colour2;
            b.FontColour = spbrand.FontColour;

            b.Quote = spbrand.Quote;
            b.Intro = spbrand.IntroParagraph;

            if (spbrand.PriorityOutletFit1 != null)
                b.OutletFit1Id = spbrand.PriorityOutletFit1.Id.GetValueOrDefault(-1);
            if(spbrand.PriorityOutletFit2 != null)
                b.OutletFit2Id = spbrand.PriorityOutletFit2.Id.GetValueOrDefault(-1);
            if(spbrand.PriorityOutletFit3 != null)
                b.OutletFit3Id = spbrand.PriorityOutletFit3.Id.GetValueOrDefault(-1);

            if (spbrand.BrandCategory != null)
                b.BrandCategoryId = Converters.ConvertNullableInt(spbrand.BrandCategory.Id);  
            if(spbrand.ImageLink != null)
            {
                var orderedLinks = spbrand.ImageLink.OrderByDescending(l => l.Order);

                foreach(ImageLink link in orderedLinks)
                {
                    string strUrl = link.URL.IndexOf(',') > 0 ? link.URL.Split(',')[0].Trim() : "";
                    string strTitle = link.URL.IndexOf(',') > 0 ? link.URL.Split(',')[1].Trim() : "";
                    ImageLinkItem ili = b.ImageLinkList;
                    ili = new ImageLinkItem(link.Id, strTitle, link.Path, DateTime.MinValue, DateTime.MinValue);
                    ili.Url = strUrl;
                    ili.Order = (int)Converters.ConvertNullableDouble(link.Order);
                    SPFieldUrlValue spOnTheInternetLogo = link.Logo as SPFieldUrlValue;
                    if (spOnTheInternetLogo != null)
                        ili.Logo = new ImageDownload(ili.Id, spOnTheInternetLogo.Description, spOnTheInternetLogo.Url, ili.Created, ili.Modified);
                    
                }
            }

            Add(b);
        }

        public void Add(List<SPBrand> spbrands)
        {
            foreach (SPBrand spbrand in spbrands)
            {
                Add(spbrand);
            }
        }
       
    }
}

Open in new window

0
 
aspnet-scotlandAuthor Commented:
I guess I'm not too sure what you mean by "Add this item to the collection ImageLinkList property as defined in my previous comment"?

Thanks.
0
 
guru_samiCommented:
I mean :  b.ImageLinkList.Add(ili);
foreach(ImageLink link in orderedLinks)
                {
                    string strUrl = link.URL.IndexOf(',') > 0 ? link.URL.Split(',')[0].Trim() : "";
                    string strTitle = link.URL.IndexOf(',') > 0 ? link.URL.Split(',')[1].Trim() : "";
                    ImageLinkItem ili = b.ImageLinkList;
                    ili = new ImageLinkItem(link.Id, strTitle, link.Path, DateTime.MinValue, DateTime.MinValue);
                    ili.Url = strUrl;
                    ili.Order = (int)Converters.ConvertNullableDouble(link.Order);
                    SPFieldUrlValue spOnTheInternetLogo = link.Logo as SPFieldUrlValue;
                    if (spOnTheInternetLogo != null)
                        ili.Logo = new ImageDownload(ili.Id, spOnTheInternetLogo.Description, spOnTheInternetLogo.Url, ili.Created, ili.Modified);

                    b.ImageLinkList.Add(ili); //add the linkitem to your linkcollection

                }

Open in new window

0
 
aspnet-scotlandAuthor Commented:
Thanks guru_sami but I still seem to receive error "Cannot implicitly convert type 'ImageLinkCollection' to target type 'ImageLinkItem'" on line 5 of your attached code?

Thanks.
0
All Courses

From novice to tech pro — start learning today.