Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How do I work with collections in c#?

Posted on 2011-09-21
8
Medium Priority
?
309 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: 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 2000 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

Congratulations! You’re Certified – Now What?

Starting a new career can be overwhelming. Becoming certified in your field of expertise is a great start, but where do you go from here?  Here are some tips to help you on your career journey.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

721 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