我的字符串是
35.318073; 65.741196; 57.335339; 87.758453; +0.573 ;; 42.282722; 92.314201; 64.299988; 114.331459; +0.537 ;; 89.842690; 109.230721; 111.859947; 131.247986; +0.644 ;;
我正在使用以下代码对其进行拆分,然后将其转换为JSON对象,然后将其转换为double对象。
if (readByCount > 30)
{
var output = (new string(buff).TrimEnd('\u0000'));
Console.WriteLine(output);
var output1 = output.Split(new[] { ";;" },StringSplitOptions.RemoveEmptyEntries).Select(s => s.Split(';')).ToArray();
JToken jsonParsed = JToken.FromObject(output1);
List<double> zcoordinate = new List<double>();
double zaxis = 0.0;
double[] MediumArray = { 0,0 };
double zCoordinateMin = 0.0;
foreach (var arrayItem in jsonParsed)
{
var innerArray = arrayItem.ToObject<double[]>();
MediumArray.Append<double[]>(innerArray);
zaxis = innerArray.ElementAt<double>(4);
zcoordinate.Add(zaxis);
foreach (var item in zcoordinate)
{
zCoordinateMin = zcoordinate.Min();
}
}
for (int i = 0; i < MediumArray.Length; i++)
{
if (MediumArray.ElementAt(i) == zCoordinateMin)
{
Console.WriteLine(MediumArray[0] + MediumArray[1]);
}
}
如您所见,由于输入字符串的格式,for-each循环将运行三次。我想将我的MediumArray设为一个数组的数组,以便我可以运行foreach循环以仅针对该元素的最小值位于位置5(索引4)的该数组找到MediumArray [0] + MediumArray [1] )。
我知道我是否具有mediumArray数组的数组,我不能使用代码中显示的for循环,但是我必须使用for-each循环,例如:
//foreach (var item in MediumArray)
//{
// if (MediumArray.ElementAt(4) == zCoordinateMin)
// {
// Console.WriteLine(MediumArray[0] + MediumArray[1]);
// }
//}
我正在尝试此代码,但由于此行而出现错误。
MediumArray.Append<double[]>(innerArray);
错误说:
'double []'不包含对'Append'和最好的定义扩展方法重载'Enumerable.Append(IEnumerable,double [])'需要类型为“ IEnumerable”的接收器]
我该怎么做才能正确执行该过程?
这是我想到的一个Linq查询,可以完成这项工作:
var input = "35.318073;65.741196;57.335339;87.758453;+0.573;;42.282722;92.314201;64.299988;114.331459;+0.537;;89.842690;109.230721;111.859947;131.247986;+0.644;;";
var output = input
.Split(";;") // split input string into the double arrays
.Where(array => !string.IsNullOrEmpty(array)) // ignore empty array (last one in your example)
.Select(array => array
.Split(";") // split into single values
.Select(value => double.Parse(value)) // parse them to doubles
.ToArray()) // join them together as an array
.ToArray(); // join the arrays together as an array
此查询返回System.Double[][]
。