简体   繁体   English

元组列表到该元组列表C#中的第一个元素列表

[英]List of tuples to list of first elements in that list of tuple C#

In Dictionary I can get a list of its Key / Values like in the below code: Dictionary我可以获取其Key / Values的列表,如以下代码所示:

    Dictionary<string, string> dictionary = new Dictionary<string, string>();
    List<string> list_of_first_elements = new List<string>();
    list_of_first_elements = new List<string> (dictionary.Keys);

Is there an attribute/method can do same with List<Tuple> without looping over the list and add its first/second elements? 是否有一个属性/方法可以与List<Tuple>相同,而无需遍历列表并添加其第一/第二个元素? Something like that: 像这样:

        List<Tuple<string, string>> list_of_tuples = new List<Tuple<string, string>>();
        list_of_first_elements = """SomeCode""";

正如@KlausGütter和@ 2kay提到的那样,我使用LINQ来做到这一点,它的性能几乎可以遍历整个List但是它显然更加清晰,下面是代码:

list_of_first_elements = list_of_tuples.Select(_ => _.Item1).ToList();

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

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