Solved

How do I work with collections in c#?

Posted on 2011-09-21
8
284 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:aspnet-scotland
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

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…
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.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

744 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

16 Experts available now in Live!

Get 1:1 Help Now