当我尝试使用.ToDictionary()将Dictionary<string, string>
转换为Dictionary<string, IEnumerable<string>>
时出现错误
public Dictionary<string, IEnumerable<string>> ConvertDictionary(Dictionary<string, string> data)
{
return data.ToDictionary(x => x.Key, y => new List<string> { y.Value });
}
我得到的错误是“无法将Dictionary<string, List<string>>
隐式转换为Dictionary<string, IEnumerable<string>>
我尝试过,但是您不能创建抽象类型的实例
return data.ToDictionary(x => x.Key, y => new IEnumerable<string> { y.Value });
我尝试过这种方法,但是.ToDictionary()不允许您这样指定类型
return data.ToDictionary<string, IEnumerable<string>>(x => x.Key, y => new List<string> { y.Value });
答案 0 :(得分:0)
在Linq语句中使用“ as”进行转换
public Dictionary<string, IEnumerable<string>> ConvertDictionary(Dictionary<string, string> data)
{
return data.ToDictionary(x => x.Key, y => new List<string> { y.Value } as IEnumerable<string>);
}