在List <tuple <string,string =“”>&gt;中创建Foreach循环

时间:2017-02-16 18:34:22

标签: tuples

我想遍历我的列表并创建Tuple从该列表中添加项目。我不能让它发挥作用

var arr = "PAR: Parent,SVE: School visit from an engineer,SCI: Science teacher";    
var query = arr.Split(',').Select(x => x.Split(':'))
                         .ToDictionary(x => x[0], x => x[1]);   

var list = query.ToList();  

    var tupleList = new List<Tuple<string, string>>
{    
foreach (var i in list )
        {
            Tuple.Create(i.Key, i.Value);
        }
    };

1 个答案:

答案 0 :(得分:1)

您需要的是

var tupleList = new List<Tuple<string, string>>();
foreach (var i in list)
{
    tupleList.Add(Tuple.Create(i.Key, i.Value));
}

收集初始化程序,例如

var tupleList = new List<Tuple<string, string>>
{
    foreach (var i in list)
    {
        Tuple.Create(i.Key, i.Value);
    }
} 

无效。