Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Jin Botol 134 posts 287 karma points
    Feb 06, 2019 @ 09:08
    Jin Botol
    0

    How to get 3 values using KeyValuePair

    Hi All,

    Sorry for noob question.

    Here is my problem, how can I get the image url for each product?

    System.Web.Http.HttpGet]
    public List<KeyValuePair<int, string>> ComparisonKeyValues(string ids)
    {
        if (!string.IsNullOrEmpty(ids))
        {
            var nodes = ids.Split(',').ToList().TypedContentList();
            return nodes.Select(x => new KeyValuePair<int, string>(x.Id, x.GetPropertyValue<string>("title"))).ToList();
    
            // I want to get also the x.GetPropertyValue<string>(productImage).Url
        }
        return new List<KeyValuePair<int, string>>();
    }
    

    Any help for this?

    Thanks in advance.

    I am new in C#, and also in Umbraco.

    Jin

  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    Feb 06, 2019 @ 09:53
    Dirk De Grave
    0

    Hi,

    Depends... what is productImage ? A property, an object (don't think this code will compile, don't see productImage defined somewhere?

    If it's a property, which I'm assuming ,because you're using x.GetPropertyValue(), then it will still depend on how productImage property was defined? Either as upload or a media picker?

    If the first, then

    x.GetPropertyValue<string>("productImage") 
    

    will already return path to the image, otherwise, you'd need to

    Umbraco.TypedMedia(x.GetPropertyValue<string>("productImage") 
    

    which returns a IPublishedContent, from where you can do .Url to get the path to the image

  • Jin Botol 134 posts 287 karma points
    Feb 07, 2019 @ 02:42
    Jin Botol
    0

    Hi Dirk,

    Yes it's a property, but when I add x.GetPropertyValue<string>("productImage") it says KeyValuePair does not contain a constructor that takes 3 arguments.

    return nodes.Select(x => new KeyValuePair<int, string>(x.Id, x.GetPropertyValue<string>("title"), x.GetPropertyValue<string>("productImage")).ToList();
    

    I want to return ID, title, and Url.

    Thanks for your response.

    Jin

  • Jin Botol 134 posts 287 karma points
    Feb 07, 2019 @ 06:59
    Jin Botol
    0

    Or you have any idea on how I can get all the properties using their ids

    public List<KeyValuePair<int, string>> ComparisonKeyValues(string ids)
    {
        if (!string.IsNullOrEmpty(ids))
        {
            var nodes = ids.Split(',').ToList().TypedContentList();
            return nodes.Select(x => new KeyValuePair<int, string>(x.Id, x.GetPropertyValue<string>("title"))).ToList();
        }
        return new List<KeyValuePair<int, string>>();
    }
    

    I want to get also the productImage property.

    Any help for this, I'm stuck on this problem.

    Thanks in advance,

    Jin

  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    Feb 07, 2019 @ 09:32
    Dirk De Grave
    1

    Hi,

    if you want to use the current format of KeyValuePair<>, you'd have to extend the second generic type to accept multiple values, so would be something along the lines of

    List<KeyValuePair<int, Dictionary<string, string>>>
    

    which I believe is a bit overkill, why not create a specific class holding three properties and return as

    List<Data>()
    

    Example:

    public class Data {
      int Id {get;set;}
      string Title {get;set;}
      string ProductImageUrl {get;set;}
    }
    

    and return a

    public List<Data> ComparisonKeyValues(string ids) {}
    

    from your method

  • Marky 26 posts 65 karma points
    Feb 07, 2019 @ 09:50
    Marky
    0

    Hi Dirk

    What do you mean by overkill??

    Like this?

    [System.Web.Http.HttpGet]
    public List<Compares> CompareValues(string ids)
    {
        var result = new List<Compares>();
    
        if (!string.IsNullOrEmpty(ids))
        {
            var nodes = ids.Split(',').ToList().TypedContentList();
            return nodes.Select(x => new KeyValuePair<int, string>(x.Id, x.GetPropertyValue<string>("title"))).ToList();
            /// Error : Cannot implicity convert type 'System.Collections.Generic.List....
    
            // what should I put in here?
        }
    
        return result;
    }
    

    Thanks dude.

    Marky

  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    Feb 07, 2019 @ 09:53
    Dirk De Grave
    0

    Overkill = list of keyvaluepair where value is again a dictionary, bit too complex for what you'd need (and doesn't make it easier to read)

    return nodes.Select(x => new Compares { Id = x.Id, Title = x.GetPropertyValue<string>("title"), ProductImageUrl = ... });
    
  • Marky 26 posts 65 karma points
    Feb 07, 2019 @ 10:02
    Marky
    0

    Okay..

    Is still got an error

    return nodes.Select(x => new Compares { Id = x.Id, Title = x.GetPropertyValue<string>("title"), Url = x.GetPropertyValue<string>("productImage") });
    

    Cannot implicity convert type 'System.Collections.Generic.IEnumerable

    Thanks,

    Marky

  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    Feb 07, 2019 @ 10:03
    Dirk De Grave
    0

    Because nodes.Select returns IEnumerable

  • Marky 26 posts 65 karma points
    Feb 07, 2019 @ 10:06
    Marky
    0

    Oh,

    How can I rid of this? Any alternative way?

    Sorry for my noob question, I really new in C# & Umbraco :(

    Marky

  • Marky 26 posts 65 karma points
    Feb 09, 2019 @ 03:51
    Marky
    0

    Hi Dirk

    I've got the correct code, thanks for your help.

    Here what I've done.

    public List<Compares> CompareValues(string ids)
    {
        var result = new List<Compares>();
    
        if (!string.IsNullOrEmpty(ids))
        {
            string[] listOdIds = ids.Split(new char[] { ',' });
    
            foreach (var id in listOdIds)
            {
                var page = Umbraco.TypedContent(id);
                var imageUrl = Umbraco.TypedMedia(page.GetPropertyValue<string>("image")).Url;
    
                var comparess = new Compares()
                {
                    Id = Convert.ToInt32(id),
                    Title = page.GetPropertyValue<string>("title"),
                    Url = imageUrl
                };
    
                result.Add(comparess);
            }
            return result;
        }
        return result;
    }
    

    Thanks,

    Marky

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies