Solved

How do I work with collections in c#?

Posted on 2011-09-21
8
295 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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
 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

685 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