Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/320.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#列表到字典,无Foreach[错误]_C#_.net_Linq_Generics_Lambda - Fatal编程技术网

C#列表到字典,无Foreach[错误]

C#列表到字典,无Foreach[错误],c#,.net,linq,generics,lambda,C#,.net,Linq,Generics,Lambda,我有这门课 class Person { public string idCard { get; set; } public string name { get; set; } public DateTime birthDate { get; set; } } 和对象列表 List<Person> list = new List<Person>(){ new Person(){name="John",idCard="123",birthD

我有这门课

class Person
{
    public string idCard { get; set; }
    public string name { get; set; }
    public DateTime birthDate { get; set; }
}
和对象列表

List<Person> list = new List<Person>(){
    new Person(){name="John",idCard="123",birthDate=new DateTime(1990,11,13)},
    new Person(){name="Paul",idCard="321",birthDate=new DateTime(1988,5,16)},
    new Person(){name="Clara",idCard="213",birthDate=new DateTime(1993,7,21)}
};
List List=新列表(){
新人(){name=“John”,idCard=“123”,生日=新的日期时间(1990,11,13)},
新人(){name=“Paul”,idCard=“321”,生日=新的日期时间(1988,5,16)},
新人(){name=“Clara”,idCard=“213”,生日=新的日期时间(1993,7,21)}
};
当我想在不使用foreach的情况下将该列表转换为dictionary时,其中对象的一个属性是键(我已经在web上搜索了所有的方法)

Dictionary personDict=newdictionary();
personDict=list.GroupBy(x=>x.idCard).ToDictionary(x=>x.Key,x=>x);
我仍然有一些错误

错误1实例参数:无法从“System.Collections.Generic.IEnumerable>”转换为“System.Collections.Generic.IEnumerable”G:\SHUBA\Learning\Experiments\TestProgram\TestProgram\Program.cs 25 26 TestProgram 错误4参数3:无法从“lambda表达式”转换为“System.Collections.Generic.IEqualityComparer”G:\SHUBA\Learning\Experiments\TestProgram\TestProgram.cs 25 95 TestProgram 错误3参数2:无法从“lambda表达式”转换为“System.Func”G:\SHUBA\Learning\Experiments\TestProgram\TestProgram\Program.cs 25 83 TestProgram 错误2“System.Collections.Generic.IEnumerable>”不包含“ToDictionary”和“System.Linq.Enumerable.ToDictionary”的最佳扩展方法重载的定义(System.Collections.Generic.IEnumerable、System.Func、System.Collections.Generic.IEqualityComparer)'有一些无效参数G:\SHUBA\Learning\Experiments\TestProgram\TestProgram\Program.cs 25 26 TestProgram

有人知道解决办法吗


我想我是对的。

分组是不必要的,
ToDictionary
为您做这件事。这应该很简单:

var personDict = list.ToDictionary(p => p.idCard);

顺便说一句,在第一步中不需要声明变量personDict并将其分配给空字典,稍后再重新分配一行。

这将为您提供一个字典,其中键是
idCard
,值是
Person
对象

Dictionary<string, Person> personDict = list.ToDictionary(x => x.idCard, y => y);
Dictionary personDict=list.ToDictionary(x=>x.idCard,y=>y);

由于我们将把
IdCard
值作为字典键,因此如果列表中有多个项具有相同的
IdCard
值,则方法调用将抛出一个
ArgumentException

我只是认为linq或lambda性能更好。如果我有10.000.000个对象,我肯定它会减慢进程。你是否尝试过,不要假设,而是测量。如果你尝试测量ToDictionary解决方案和简单foreach的性能,你会发现foreach总是更快。我刚刚尝试过,对于10.000.000个数据,ToDictionary需要3.021174,当foreach消耗4.1892389时,当然,当我不知道为什么我错了的时候,我无法尝试。我在下面的答案部分找到解决方案后尝试了。谢谢所有正确回答我问题的人。哦,我的。。。太尴尬了。我想我做的太多了。无论如何,非常感谢你
Dictionary<string, Person> personDict = list.ToDictionary(x => x.idCard, y => y);