将哈希表转换为对象列表
Convert Hashtable to List of Objects
我们如何将哈希表转换为对象列表?可能吗?
业务对象:-
[Serializable]
public class ColourEntry
{
public string Id
{
get { return this.id; }
set { this.id= value; }
}
public string Name
{
get { return this.name; }
set { this.name= value; }
}
public Hashtable Properties
{
get { return this.properties; }
set { this.properties = value; }
}
}
数据合同:-
[DataContract(Name = "Color", Namespace = Constants.Namespace)]
public class ColorContract
{
[DataMember(EmitDefaultValue = false)]
public string Id { get; set; }
[DataMember(EmitDefaultValue = false)]
public string Name { get; set; }
[DataMember(EmitDefaultValue = false)]
public List<PropertiesContract> Properties { get; set; }
}
[DataContract(Name = "Properties", Namespace = Constants.Namespace)]
public class PropertiesContract
{
[DataMember(EmitDefaultValue = false)]
public string Key { get; set; }
[DataMember(EmitDefaultValue = false)]
public string Value { get; set; }
}
业务对象到数据协定 Mapper 函数:-
public static List<ColorContract> MapContract(IList<ColourEntry> colourEntryList)
{
var colorContract = colourEntryList.Select(x => new ColorContract()
{
Id = x.Id.ToDisplayString(),
Name = x.Name,
Properties = x.Properties
}
return colorContract;
}
这给了我
的错误
"Error Cannot implicitly convert type 'System.Collections.Hashtable'
to
'System.Collections.Generic.List'"
因为 ColorEntry 是具有哈希表属性的对象。
我也试过 x.Properties.ToList() 但这些也不起作用。
代码中的 HashTable
在哪里?我假设它是 ColourEntry
class 中的 属性 Properties
。所以您想将 HashTable
转换为 List<PropertiesContract>
.
我想这就是你想要的 (hashTable.Cast<DictionaryEntry>
):
var colorContract = colourEntryList.Select(x => new ColorContract()
{
Id = x.Id.ToDisplayString(),
Name = x.Name,
Properties = x.Properties
.Cast<DictionaryEntry>()
.Select(kv => new PropertiesContract{ Key = kv.Key.ToString(), Value = kv.Value?.ToString() })
.ToList()
}
return colorContract;
我们如何将哈希表转换为对象列表?可能吗?
业务对象:-
[Serializable]
public class ColourEntry
{
public string Id
{
get { return this.id; }
set { this.id= value; }
}
public string Name
{
get { return this.name; }
set { this.name= value; }
}
public Hashtable Properties
{
get { return this.properties; }
set { this.properties = value; }
}
}
数据合同:-
[DataContract(Name = "Color", Namespace = Constants.Namespace)]
public class ColorContract
{
[DataMember(EmitDefaultValue = false)]
public string Id { get; set; }
[DataMember(EmitDefaultValue = false)]
public string Name { get; set; }
[DataMember(EmitDefaultValue = false)]
public List<PropertiesContract> Properties { get; set; }
}
[DataContract(Name = "Properties", Namespace = Constants.Namespace)]
public class PropertiesContract
{
[DataMember(EmitDefaultValue = false)]
public string Key { get; set; }
[DataMember(EmitDefaultValue = false)]
public string Value { get; set; }
}
业务对象到数据协定 Mapper 函数:-
public static List<ColorContract> MapContract(IList<ColourEntry> colourEntryList)
{
var colorContract = colourEntryList.Select(x => new ColorContract()
{
Id = x.Id.ToDisplayString(),
Name = x.Name,
Properties = x.Properties
}
return colorContract;
}
这给了我
的错误"Error Cannot implicitly convert type 'System.Collections.Hashtable' to 'System.Collections.Generic.List'"
因为 ColorEntry 是具有哈希表属性的对象。
我也试过 x.Properties.ToList() 但这些也不起作用。
代码中的 HashTable
在哪里?我假设它是 ColourEntry
class 中的 属性 Properties
。所以您想将 HashTable
转换为 List<PropertiesContract>
.
我想这就是你想要的 (hashTable.Cast<DictionaryEntry>
):
var colorContract = colourEntryList.Select(x => new ColorContract()
{
Id = x.Id.ToDisplayString(),
Name = x.Name,
Properties = x.Properties
.Cast<DictionaryEntry>()
.Select(kv => new PropertiesContract{ Key = kv.Key.ToString(), Value = kv.Value?.ToString() })
.ToList()
}
return colorContract;