在下面的代码,我需要明确提到CountryId
和CountryName
但我想,以避免和试图建立一个generic method
。转换一个通用的IEnumerable <T>到了IEnumerable <KeyValuePair>(C#)
public struct KeyValueStruct
{
public int Key { get; set; }
public string Value { get; set; }
}
private static IEnumerable<KeyValueStruct> ConvertPocoToKeyValueList(IEnumerable<CountryPoco> list)
{
var result = new List<KeyValueStruct>();
if (list != null)
{
foreach (var item in list)
{
result.Add(new KeyValueStruct()
{
Key = item.CountryId,
Value = item.CountryName
});
}
}
return result;
}
我从列表中知道第一个属性总是整数(在本例中是CountryId),第二个属性是String。
我想使用Generics
来实现,但我不确定这是否是最好的方法,请参阅我的建议代码(虽然它不工作)。
private static IEnumerable<KeyValueStruct> ConvertPocoToKeyValueList<T>(T list)
{
var result = new List<KeyValueStruct>();
if (list != null)
{
foreach (var item in list)
{
result.Add(new KeyValueStruct()
{
Key = item.CountryId,
Value = item.CountryName
});
}
}
return result;
}
如果您有更好的主意来达到同样的效果,那么请提出建议。
什么是KeyValueStruct?任何你不使用.NET框架中的KeyValuePair的理由?请注意,LINQ使所有这些微不足道的,btw ...'var result = countries.Select(c => new KeyValuePair (c.CountryId,c.CountryName).ToList();'当然,你需要处理输入为空,也许......但是你可能会发现,防止这种情况发生会更好。 –
@JonSkeet:我意识到我错过了KeyValueStruct,并且刚刚添加到问题中。具有自定义结构在性能方面要快得多,所以我决定自己实现,而不是使用.NET的默认KeyValuePair。有没有办法避免对'c.CountryId,c.CountryName'进行硬编码? –
Um ,'KeyValuePair'已经是一个结构体......你认为你自己的'KeyValueStruct'类型的速度更快吗?无可否认这是一个可变值类型,这是一个不同的...但不是一个积极的类型 –