【发布时间】:2018-08-16 23:50:20
【问题描述】:
我需要从IDictionary<int,string> 中选择一个数据子集来匹配来自IEnumerable<T> 的键,然后返回一个新的IEnumerable<T>。我被语法困住并得到错误:
方法
Extensions.GetValues<K, V>(IDictionary<K, V>, IEnumerable<K>)的类型参数不能从 用法。尝试明确指定类型参数。
这是我的代码:
public class Subject
{
public short SubjectID { get; set; }
public byte CategoryID { get; set; }
public string Title { get; set; }
}
public static class Extensions
{
public static IEnumerable<V> GetValues<K, V>(this IDictionary<K, V> dict, IEnumerable<K> keys)
{
return keys.Select((x) => dict[x]);
}
}
private IEnumerable<Subject> Translate()
{
IEnumerable<Subject> selectedSubjects = new Subject[] { new Subject { SubjectID = 1, CategoryID = 2, Title = null } };
// this is given externally as an IDictionary
var dict = new Dictionary<int, string>
{
{ 1, "Hello" },
{ 2, "Goodbye" }
};
// this line produces the error:
IEnumerable<Subject> data = dict.GetValues(selectedSubjects);
// would like to return IEnumerable<Subject> containing SubjectID = 1, CategoryID and Title = "Hello"
return data;
}
我想我需要告诉它以某种方式使用SubjectID 作为short 过滤dict?
【问题讨论】: