问题描述
我想知道是否有人知道写这个的好方法.我有一个键值对列表.键是一个简单的字符串,值是一个字符串列表.我正在尝试将其写到输出文件中,如下所示:
I was wondering if anyone knows a good way of writing this. I have a list of key value pairs. The key is a simple string and the value is a list of strings. I'm trying to write it out to an output file like so:
File.WriteAllLines(@"C:\Users\S\Downloads\output.txt",
xEleAtt.Select(x => x.Key + " Val's: " + x.Value).ToArray());
但是我得到的输出(有点我想会发生的)是这样的:
However the output I get (which is kinda what I thought would happen) is this:
Queue0 Val:System.Collections.Generic.List`1 [System.String]
Queue0 Val's: System.Collections.Generic.List`1[System.String]
Queue1 Val:System.Collections.Generic.List`1 [System.String]
Queue1 Val's: System.Collections.Generic.List`1[System.String]
Queue2 Val的:System.Collections.Generic.List`1 [System.String]
Queue2 Val's: System.Collections.Generic.List`1[System.String]
有没有人知道我如何使用以我写的方式编写的linq打印列表的内容?
Has anyone got an idea of how I can print the contents of the list using linq written in the way i've wrote it?
推荐答案
您可以使用 String.Join
将您的List<string>
连接到具有给定分隔符的单个string
中:
You could use String.Join
to concatenate your List<string>
into a single string
with a given delimiter:
File.WriteAllLines(@"C:\Users\S\Downloads\output.txt",
xEleAtt.Select(x => x.Key + " Val's: " +
string.Join(",", x.Value.ToArray()).ToArray());
这篇关于写入List< KeyValuePair< string,List< string>>到文本文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!