简体   繁体   English

如何使用LINQ填充字典?

[英]How to populate Dictionary using LINQ?

public class Person
{
     public string Email {get; set;}
     public string Name {get; set;}
}

public Report : Dictionary<string, string>
{
     public Report()
     {
         List<Person> list = getData(); // filled with Person's
         var realList = list.Where(x => x.Name != "test");
         foreach( var i in realList)
         {
              this.Add(i.Email,i.Name);
         }
     }

}

I tried to simplify my code to get down to the question. 我试图简化我的代码以解决问题。 Basically I have a list of Persons and I want to get a sub list of that and then for each element in the sub list I want to add it to this dictionary which is the class. 基本上我有一个人员列表,我想得到一个子列表,然后对于子列表中的每个元素,我想将它添加到这个类的字典中。

My question: Is there a way to get rid of that foreach statement and achieve the same effect with in the linq statement above. 我的问题:有没有办法摆脱那个foreach语句,并在上面的linq语句中实现相同的效果。 Because it seems silly to me to go through the list once and then through the sub list. 因为看一次然后再通过子列表对我来说似乎很愚蠢。 So what would be good if during the linq statement if x.Name is not equal to test then it adds it to the dictionary. 那么如果在linq语句中如果x.Name不等于test那么它会将它添加到字典中会有什么好处。

My question: Is there a way to get rid of that foreach statement and achieve the same effect with in the linq statement above. 我的问题:有没有办法摆脱那个foreach语句,并在上面的linq语句中实现相同的效果。 Because it seems silly to me to go through the list twice 因为我两次查看清单似乎很愚蠢

You don't go through the list twice in your code - the list is only enumerated once, as LINQ uses deferred execution. 您没有在代码中两次浏览列表 - 列表仅枚举一次,因为LINQ使用延迟执行。 This is likely the best way to achieve this. 这可能是实现这一目标的最佳方式。

If you didn't subclass a dictionary, however, and encapsulated one instead, you could use ToDictionary to create the encapsulated dictionary: 但是,如果您没有对字典进行子类化,而是封装了一个字典,则可以使用ToDictionary来创建封装字典:

List<Person> list = getData(); // filled with Person's
Dictionary<string,string> people = list
                                     .Where(x => x.Name != "test")
                                     .ToDictionary(x => x.Email, x => x.Name);

Even though there are some nice answers already here how to convert your list directly to a dictionary, they don't really answer the original question, because you can't re-assign the this variable (for classes it is read-only in C#) and you will have to iterate over the newly created dictionary to add elements from it to your this dictionary. 虽然这里已经有一些很好的答案如何将列表直接转换为字典,但它们并没有真正回答原始问题, 因为你不能重新分配this变量 (对于C#中只读它的类是# ),你将不得不遍历新创建的字典从中元素添加到您的this字典。

I usually solve this problem by materializing the LINQ expression to a List and using ForEach() method of the list (that also accepts lambda-expressions) to populate another structure. 我通常通过将LINQ表达式实现为List并使用列表的ForEach()方法(也接受lambda表达式)来填充另一个结构来解决此问题。

Here isthea sample code: 这是一个示例代码:

     list
        .Where(x => x.Name != "test")             // your condition
        .ToList()                                 // turns expression to list
        .ForEach(a => this.Add(a.Email, a.Name)); // populating dictionary

You can have several conditions before ToList() invocation, just in case you want to exclude the values that are already in the dictionary. ToList()调用之前,您可以有多个条件,以防您想要排除字典中已有的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM