Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将列表转换为列表<;字典>;c语言中无foreach高效#_C#_Asp.net_Performance - Fatal编程技术网

C# 将列表转换为列表<;字典>;c语言中无foreach高效#

C# 将列表转换为列表<;字典>;c语言中无foreach高效#,c#,asp.net,performance,C#,Asp.net,Performance,假设我有一张班级名单 public class Person { public int Id { get; set; } public string Name { get; set; } public int Age { get; set; } } 现在我需要转换成字典列表,如 假设它是ListPersonDict,下面是相同的每个索引的结构。密钥名称应作为类Person中的属性名称动态填充 key : "Id", Value : Person.Id key : "N

假设我有一张班级名单

public class Person 
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int Age { get; set; }
}
现在我需要转换成字典列表,如
假设它是
ListPersonDict
,下面是相同的每个索引的结构。密钥名称应作为类Person中的属性名称动态填充

key : "Id", Value : Person.Id
key : "Name", Value : Person.Name
key : "Age", Value : Person.Age

请任何人帮忙,我已经尝试了“for each”和“Parallel for each”循环,但这需要很多时间,我有300万条记录要转换,而且需要几个小时。

下面是一个可行的实现:

using System;
using System.Collections.Generic;

public class Program
{
    public static void Main()
    {
        // Create some sample records
        var persons = new List<Person>(){
            new Person(){Id = 1, Name = "Bob", Age = 30},
            new Person(){Id = 2, Name = "Jane", Age = 31},
            new Person(){Id = 3, Name = "Mary", Age = 32}
        };

        // Use Reflection to retrieve public properties
        var properties = typeof(Person).GetProperties();

        // Create a list to store the dictionaries      
        var listOfDictionary = new List<Dictionary<string, string>>();

        // For each person class
        foreach(var person in persons){
            // Create a new dictionary
            var dict = new Dictionary<string,string>();
            // For each property
            foreach(var prop in properties){
                // Add the name and value of the property to the dictionary
                dict.Add(prop.Name, prop.GetValue(person).ToString());

            }
            // Add new dictionary to our list
            listOfDictionary.Add(dict);

        }


        // Check the contents of our list
        foreach(var dict in listOfDictionary){      
            Console.WriteLine(string.Join(",", dict.Keys));
            Console.WriteLine(string.Join(",", dict.Values));
        }

    }

    public class Person 
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
    }
}
使用系统;
使用System.Collections.Generic;
公共课程
{
公共静态void Main()
{
//创建一些示例记录
var persons=新列表(){
新人(){Id=1,Name=“Bob”,年龄=30},
新人(){Id=2,Name=“Jane”,年龄=31},
新人(){Id=3,Name=“Mary”,年龄=32}
};
//使用反射检索公共属性
var properties=typeof(Person).GetProperties();
//创建一个列表来存储字典
var listOfDictionary=新列表();
//每个人的班级
foreach(var个人对个人){
//创建新词典
var dict=新字典();
//对于每个属性
foreach(属性中的var属性){
//将属性的名称和值添加到字典中
dict.Add(prop.Name,prop.GetValue(person.ToString());
}
//把新字典添加到我们的列表中
添加目录(dict);
}
//检查一下我们名单上的内容
foreach(列表中的var dict){
Console.WriteLine(string.Join(“,”,dict.Keys));
Console.WriteLine(string.Join(“,”,dict.Values));
}
}
公共阶层人士
{
公共int Id{get;set;}
公共字符串名称{get;set;}
公共整数{get;set;}
}
}

您提到有数百万条记录需要转换。创建数百万个
字典
实例,或者将它们全部保存在内存中,这可能不是最好的办法。但是,如果不知道您的最终目标是什么,就很难推荐一些东西。

下面是一个可行的实现:

using System;
using System.Collections.Generic;

public class Program
{
    public static void Main()
    {
        // Create some sample records
        var persons = new List<Person>(){
            new Person(){Id = 1, Name = "Bob", Age = 30},
            new Person(){Id = 2, Name = "Jane", Age = 31},
            new Person(){Id = 3, Name = "Mary", Age = 32}
        };

        // Use Reflection to retrieve public properties
        var properties = typeof(Person).GetProperties();

        // Create a list to store the dictionaries      
        var listOfDictionary = new List<Dictionary<string, string>>();

        // For each person class
        foreach(var person in persons){
            // Create a new dictionary
            var dict = new Dictionary<string,string>();
            // For each property
            foreach(var prop in properties){
                // Add the name and value of the property to the dictionary
                dict.Add(prop.Name, prop.GetValue(person).ToString());

            }
            // Add new dictionary to our list
            listOfDictionary.Add(dict);

        }


        // Check the contents of our list
        foreach(var dict in listOfDictionary){      
            Console.WriteLine(string.Join(",", dict.Keys));
            Console.WriteLine(string.Join(",", dict.Values));
        }

    }

    public class Person 
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
    }
}
使用系统;
使用System.Collections.Generic;
公共课程
{
公共静态void Main()
{
//创建一些示例记录
var persons=新列表(){
新人(){Id=1,Name=“Bob”,年龄=30},
新人(){Id=2,Name=“Jane”,年龄=31},
新人(){Id=3,Name=“Mary”,年龄=32}
};
//使用反射检索公共属性
var properties=typeof(Person).GetProperties();
//创建一个列表来存储字典
var listOfDictionary=新列表();
//每个人的班级
foreach(var个人对个人){
//创建新词典
var dict=新字典();
//对于每个属性
foreach(属性中的var属性){
//将属性的名称和值添加到字典中
dict.Add(prop.Name,prop.GetValue(person.ToString());
}
//把新字典添加到我们的列表中
添加目录(dict);
}
//检查一下我们名单上的内容
foreach(列表中的var dict){
Console.WriteLine(string.Join(“,”,dict.Keys));
Console.WriteLine(string.Join(“,”,dict.Values));
}
}
公共阶层人士
{
公共int Id{get;set;}
公共字符串名称{get;set;}
公共整数{get;set;}
}
}

您提到有数百万条记录需要转换。创建数百万个
字典
实例,或者将它们全部保存在内存中,这可能不是最好的办法。然而,在不知道你的最终目标是什么的情况下,很难推荐一些东西。

这与这里的其他答案几乎相同,但使用了稍微简洁的语法。只是偏好的问题

List persons=新列表{
新人{Id=1,Name=“Sally”,年龄=10},
新人{Id=2,Name=“Bob”,年龄=9},
};
字典列表=
人
.选择(p=>new Dictionary
{
[“Id”]=p.Id.ToString(),
[“名称”]=p.名称,
[“Age”]=p.Age.ToString(),
})
.ToList();

这与此处的其他答案几乎相同,但使用了稍微简洁的语法。只是偏好的问题

List persons=新列表{
新人{Id=1,Name=“Sally”,年龄=10},
新人{Id=2,Name=“Bob”,年龄=9},
};
字典列表=
人
.选择(p=>new Dictionary
{
[“Id”]=p.Id.ToString(),
[“名称”]=p.名称,
[“Age”]=p.Age.ToString(),
})
.ToList();

您可能需要查看。的可能重复项。只需使用
Dictionary
而不是
Dictionary
ListPersonDict
这不是有效的类型请具体说明,这是一个编码站点您的问题不太清楚,但列表有
ToDictionary
方法。@CodingYoshi,方法允许为集合的每个项创建键值对。但OP希望获得字典列表,这些字典包含原始集合项的所有属性的键值对。您可能需要查看。的可能重复项。只需使用
Dictionary
而不是
Dictionary
ListPersonDict
这不是有效的类型请具体说明,这是一个编码站点您的问题不太清楚,但列表有
ToDictionary
方法。@CodingYoshi,方法允许为集合的每个项创建键值对。但OP想得到这份名单