Convert public List<List<List<double>>> to string comma separated using C#

huangapple go评论61阅读模式
英文:

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&lt;List&lt;List&lt;double&gt;&gt;&gt; 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(&quot;,&quot;, doubles.SelectMany(i =&gt; 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&lt;List&lt;List&lt;double&gt;&gt;&gt; {
    new List&lt;List&lt;double&gt;&gt;() {new List&lt;double&gt; {-118.818984489994, 34.0137559967283}},
    new List&lt;List&lt;double&gt;&gt;() {new List&lt;double&gt; {-118.818984489994, 34.0137559967283}},
    new List&lt;List&lt;double&gt;&gt;() {new List&lt;double&gt; {-118.818984489994, 34.0137559967283}},
    new List&lt;List&lt;double&gt;&gt;() {new List&lt;double&gt; {-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&lt;List&lt;List&lt;double&gt;&gt;&gt; {
    new List&lt;List&lt;double&gt;&gt;() {new List&lt;double&gt; {-118.818984489994, 34.0137559967283}},
    new List&lt;List&lt;double&gt;&gt;() {new List&lt;double&gt; {-118.818984489994, 34.0137559967283}},
    new List&lt;List&lt;double&gt;&gt;() {new List&lt;double&gt; {-118.818984489994, 34.0137559967283}},
    new List&lt;List&lt;double&gt;&gt;() {new List&lt;double&gt; {-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(&quot;,&quot;, doubles
  .SelectMany(item =&gt; item)
  .SelectMany(item =&gt; item));

huangapple
  • 本文由 发表于 2023年2月24日 04:57:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/75550252.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定