c# - 如何从多个 Id 中获取属性值
问题描述
我有一个比较功能,它有 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”。存在显式转换(您是否缺少演员表?)
感谢任何帮助。
提前致谢。
斤
解决方案
您的函数期望返回一个List<Compares>
.
这一行:
return nodes.Select(x => new KeyValuePair<int, string>(x.Id, x.GetPropertyValue<string>("title"))).ToList();
不返回List<Compares>
,它从外观上返回一个列表KeyValuePairs<int,string>
。