英文:
Convert public List<List<List<double>>> to string comma separated using C#
问题
using System;
using System.Collections.Generic;
using System.Linq;
public class YourClassName
{
public List<List<List<double>>> doubles { get; set; }
public string ConvertToString()
{
var result = "[";
foreach (var outerList in doubles)
{
result += "\n [";
foreach (var middleList in outerList)
{
var innerListString = string.Join(", ", middleList.Select(d => d.ToString()));
result += $"[{innerListString}], ";
}
result = result.TrimEnd(new char[] { ' ', ',' }) + "],";
}
result = result.TrimEnd(new char[] { ',' }) + "\n ]";
return result;
}
}
You can create an instance of the class and use the ConvertToString
method to convert your list of doubles to the desired string format.
英文:
Using C#, I have the following list of doubles:
public List<List<List<double>>> doubles { get; set; }
I need to convert it to a string using the following format:
[
[-118.818984489994, 34.0137559967283],
[-118.806796597377, 34.0215816298725],
[-118.791432890735, 34.0163883241613],
[-118.79596686535, 34.008564864635],
[-118.808558110679, 34.0035027131376]
]
This is what I have so far but it is not working (Stores the text "System.Collections.."):
string result = string.Join(",", doubles.SelectMany(i => i));
Any help would be appreciated to convert the list of doubles to one string in this format:
[
[-118.818984489994, 34.0137559967283],
[-118.806796597377, 34.0215816298725],
[-118.791432890735, 34.0163883241613],
[-118.79596686535, 34.008564864635],
[-118.808558110679, 34.0035027131376]
]
答案1
得分: 2
将数组转化为如下形式的最简单方法是使用 Newtonsoft.Json 包将数组序列化为 JSON 字符串。看起来像这样
var input = new List<List<List<double>>> {
new List<List<double>>() {new List<double> {-118.818984489994, 34.0137559967283}},
new List<List<double>>() {new List<double> {-118.818984489994, 34.0137559967283}},
new List<List<double>>() {new List<double> {-118.818984489994, 34.0137559967283}},
new List<List<double>>() {new List<double> {-118.818984489994, 34.0137559967283}},
};
var jsonString = JsonConvert.SerializeObject(input);
Console.WriteLine(jsonString);
英文:
The easiest way to make an array look like this is to serialize the array into a json string using the Newtonsoft.Json package. Looks like that
var input = new List<List<List<double>>> {
new List<List<double>>() {new List<double> {-118.818984489994, 34.0137559967283}},
new List<List<double>>() {new List<double> {-118.818984489994, 34.0137559967283}},
new List<List<double>>() {new List<double> {-118.818984489994, 34.0137559967283}},
new List<List<double>>() {new List<double> {-118.818984489994, 34.0137559967283}},
};
var jsonString = JsonConvert.SerializeObject(input);
Console.WriteLine(jsonString);
答案2
得分: 1
你想要调用 SelectMany
两次:
string result = string.Join(",", doubles
.SelectMany(item => item)
.SelectMany(item => item));
英文:
You want to call SelectMany
twice:
string result = string.Join(",", doubles
.SelectMany(item => item)
.SelectMany(item => item));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论