Solved

How do I use C# convertion types?

Posted on 2011-09-21
5
356 Views
Last Modified: 2013-12-17
Hi,

I have the below collection...

[CollectionDataContract]
    public class ImageLinkCollection : List<ImageLinkItem>
    {
        public void Add(SPBrandImageLink spImageLinks)
        {
            string strUrl = spImageLinks.URL.IndexOf(',') > 0 ? spImageLinks.URL.Split(',')[0].Trim() : "";

            ImageLinkItem i = new ImageLinkItem(Converters.ConvertNullableInt(spImageLinks.Id), spImageLinks.Title,
                                                spImageLinks.Path,
                                                Converters.ConvertNullableDate(spImageLinks.Created),
                                                Converters.ConvertNullableDate(spImageLinks.Modified));

            SPFieldUrlValue spLogo = spImageLinks.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(spImageLinks.Order);

            Add(i);
        }
    }

I am attempting to add to the collection with the below but I receive the error "Cannot implicitly convert type System.Collections.Generic.List<SPBrandImageLink> to SPBrandImageLink"?...

var query = from SPBrandImageLink in DAL.BrandOnTheInternet.Cast<SPBrandImageLink>()
                            orderby SPBrandImageLink.Order
                            select SPBrandImageLink;

                SPBrandImageLink spImageLinks = query.ToList<SPBrandImageLink>();

                Logger.LogInfo("Found " + query.Count().ToString() + " On The Internet Links for " + HttpContext.Current.User.Identity.Name);

                result.Add(spImageLinks);
0
Comment
Question by:aspnet-scotland
5 Comments
 
LVL 10

Assisted Solution

by:oxyoo
oxyoo earned 300 total points
ID: 36572454
Instead of:  SPBrandImageLink spImageLinks = query.ToList<SPBrandImageLink>();
Try this:


IList<SPBrandImageLink> spImageLinks = query.ToList<SPBrandImageLink>();

Open in new window

0
 

Accepted Solution

by:
aspnet-scotland earned 0 total points
ID: 36572475
I also had to add the below as a method within ImageLinkCollection changing the initial Add method variable to spImageLink...

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

Thanks.
0
 
LVL 16

Assisted Solution

by:Easwaran Paramasivam
Easwaran Paramasivam earned 200 total points
ID: 36572525
Use below code:              

 List<SPBrandImageLink> spImageLinks = query.ToList<SPBrandImageLink>();

0
 
LVL 1

Expert Comment

by:RG59
ID: 36572563
You could just use a var (assuming .Net 3.5 or later) and let .Net take care of it...

var spImageLinks = query.ToList<SPBrandImageLink>();
0
 

Author Closing Comment

by:aspnet-scotland
ID: 36597764
Partially solved myself
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

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!
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

895 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