Solved

How do I work with collections in c#?

Posted on 2011-09-21
8
286 Views
Last Modified: 2013-12-17
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

0
Comment
Question by:aspnet-scotland
  • 4
  • 4
8 Comments
 
LVL 41

Expert Comment

by:guru_sami
ID: 36575941
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
 

Author Comment

by:aspnet-scotland
ID: 36577203
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
 
LVL 41

Expert Comment

by:guru_sami
ID: 36577562
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
 

Author Comment

by:aspnet-scotland
ID: 36579187
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:aspnet-scotland
ID: 36579446
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
 
LVL 41

Expert Comment

by:guru_sami
ID: 36583797
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
 

Author Comment

by:aspnet-scotland
ID: 36585695
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
 
LVL 41

Accepted Solution

by:
guru_sami earned 500 total points
ID: 36588127
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

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now