Write List>> to text file?

前端 未结 3 1891
萌比男神i
萌比男神i 2021-01-27 07:37

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

相关标签:
3条回答
  • 2021-01-27 07:39

    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());
    
    0 讨论(0)
  • 2021-01-27 07:44

    Try this:

    File.WriteAllLines(@"C:\Users\S\Downloads\output.txt",
        from kvp in input
        select kvp.Key + ": " + string.Join(", ", kvp.Value));
    
    0 讨论(0)
  • File.WriteAllLines(@"C:\Users\S\Downloads\output.txt",
             xEleAlt.SelectMany(x=> x.Value, (x,y)=> x.Key + " Val's: " + y).ToArray());
    
    //Result
    Queue0  ....
    Queue0  ....
    ......
    Queue1  ....
    Queue1  ....
    ....
    

    NOTE: I'm not sure if you want to join all the strings in the List<string> to make the value for each entry. If you want so, refer the answer of Douglas

    0 讨论(0)
提交回复
热议问题