我在c#
中有以下代码段public class Client
{
public string ID { get; set; }
public string Name { get; set; }
public string Address { get; set; }
}
var liste = new List<Dictionary<string, string>>();
var dictionary = new Dictionary<string, string>();
dictionary["Id"] = "111";
dictionary["Name"] = "XYZ";
dictionary["Address"] = "Addd";
liste.Add(dictionary);
var result = liste.SelectMany(x => x);
//Code for Converting result into List<Client>
现在我想使用linq
从结果查询创建List答案 0 :(得分:13)
嗯,你可以这样做:
var result = liste.Select(map => new Client { ID = map["ID"],
Name = map["Name"],
Address = map["Address"] })
.ToList();
这是你在想什么?您可以通过迭代字典并使用反射设置属性来使其更具通用性......当然,它会变得明显更长的代码。
答案 1 :(得分:2)
试试这个
var q = (from dic in liste
select new Client
{
Id = dic["Id"],
Name = dic["Name"],
Address = dic["Address"],
}).ToList();