【问题标题】:How to get property values from multiple Ids如何从多个 Id 中获取属性值
【发布时间】:2019-02-21 00:06:19
【问题描述】:

我有一个比较功能,它有 3 个要比较的项目。 我的问题是如何使用它们对应的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;
}

全文如下:

无法将类型“System.Collections.Generic.IEnumerable”隐式转换为“System.Collections.Generic.List”。存在显式转换(您是否缺少演员表?)

感谢任何帮助。

提前致谢。

【问题讨论】:

  • 你能把idsnodes的值贴出来吗?此外,您的预期输出。
  • 你能提供完整的错误信息吗?
  • stackoverflow.com/a/16824542/8435038 这看起来像您需要的吗?你的问题并不容易知道你想要什么哈哈。
  • @Bijay 数据:/// Umbraco/Api/Search/ComparisonKeyValues?ids=2874,2875,2876
  • @John 无法将类型 'System.Collections.Generic.IEnumerable' 隐式转换为 'System.Collections.Generic.List'。存在显式转换(您是否缺少演员表?)

标签: c# umbraco7


【解决方案1】:

您的函数期望返回List&lt;Compares&gt;

这一行:

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

不返回List&lt;Compares&gt;,而是返回KeyValuePairs&lt;int,string&gt; 的列表。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-22
    • 2013-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多