0

I have a compare function which has a 3 items to compare. My problem is how can I get their properties with their corresponding ID.

[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....
    }

    return result;
}

The full message is as follows:

Cannot implicitly convert type 'System.Collections.Generic.IEnumerable' to 'System.Collections.Generic.List'. An explicit conversion exists (are you missing a cast?)

Appreciate any help.

Thanks in advance.

Jin

ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
Jin
  • 153
  • 2
  • 12
  • Can you post the values of `ids` and `nodes`? Also, your expected output. – Bijay Yadav Feb 08 '19 at 08:35
  • Can you provide the full error message? – ProgrammingLlama Feb 08 '19 at 08:35
  • https://stackoverflow.com/a/16824542/8435038 does that look like what you need? Your question doesn't make it easy to know what you want lol. – Dave C Feb 08 '19 at 08:35
  • @Bijay data: _/// Umbraco/Api/Search/ComparisonKeyValues?ids=2874,2875,2876_ – Jin Feb 08 '19 at 08:38
  • @John _Cannot implicitly convert type 'System.Collections.Generic.IEnumerable' to 'System.Collections.Generic.List'. An explicit conversion exists (are you missing a cast?)_ – Jin Feb 08 '19 at 08:40
  • @Dave, but how can I get the IDs? sorry, I am new in C#. THanks – Jin Feb 08 '19 at 08:46
  • Once you've split them it will be a List of Strings, so you need to convert your List to List - https://stackoverflow.com/a/6201377/8435038 – Dave C Feb 08 '19 at 08:49
  • how can I split them? like this _var listOfIds = ids.Split(',').ToList()_ ? Thanks.. sorry for my noob question, I really new in C# :( – Jin Feb 08 '19 at 10:05

1 Answers1

0

Your function expects to return a List<Compares>.

This line:

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

Is NOT returning List<Compares>, it's returning a list of KeyValuePairs<int,string> by the look of it.

Tim
  • 4,217
  • 1
  • 15
  • 21