使用Linq列出<Type>的词典列表

san*_*ngh 5 c# linq

我在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>
Run Code Online (Sandbox Code Playgroud)

现在我想使用linq从结果查询中创建List

Jon*_*eet 13

好吧,你可以这样做:

var result = liste.Select(map => new Client { ID = map["ID"],
                                              Name = map["Name"],
                                              Address = map["Address"] })
                  .ToList();
Run Code Online (Sandbox Code Playgroud)

这是你在想什么?您可以通过迭代字典并使用反射设置属性来使其更具通用性......当然,它会变得明显更长的代码.

  • 该死的你在点击提交时打败了我! (2认同)