[英]How to convert array of Integers into comma separated string
This is my integer array with Ids 这是我的ID的整数数组
GoalIds{int[7]}
[0]: 31935
[1]: 31940
[2]: 31976
[3]: 31993
[4]: 31994
[5]: 31995
[6]: 31990
I am getting the above array from this code 我从这段代码中获取上面的数组
Array GoalIds = FilteredEmpGoals.Select(x => x.GoalId).Distinct().ToArray();
I am trying to convert it to comma separated string like 我正在尝试将其转换为逗号分隔的字符串
31935, 31940, 31976, 31993, 31994, 31995, 31990
To achieve this I tried 为此,我尝试了
var result = string.Join(",", GoalIds);
but its's giving me "System.Int32[]"
in result. 但结果却是
"System.Int32[]"
。
Please let me update where I make a mistake here. 请让我更新我在这里出错的地方。
Ref: I looked at here and the example is working fine from there. 参考:我在这里看了,这个例子在那儿工作得很好。
UPDATE UPDATE
REF: As @paqogomez suggested REF:如@paqogomez所建议
I was trying to store the values in a Array but may be it was not handling the values correctly. 我试图将值存储在Array中,但可能是未正确处理值。 Now I did change the code for making the array as below
现在我确实更改了用于制作数组的代码,如下所示
int[] GoalIds = FilteredEmpGoals.Select(x => x.GoalId).Distinct().ToArray();
Now it's working fine for me. 现在对我来说很好。
In declaring GoalIds
as an Array
type, you are not getting an iterator to be able to run in String.Join
. 在将
GoalIds
声明为Array
类型时,没有使迭代器能够在String.Join
运行。
Try: 尝试:
int[] GoalIds = FilteredEmpGoals.Select(x => x.GoalId).Distinct().ToArray();
var result = string.Join(",", GoalIds);
As @JeppeStigNielsen notes in the comments, this is also valid and eliminates the ToArray
call: 正如@JeppeStigNielsen在评论中指出的那样,这也是有效的,并且消除了
ToArray
调用:
var GoalIds = FilteredEmpGoals.Select(x => x.GoalId).Distinct();
I have run this code in c# and its work fine dont know what is your problem 我已经在C#中运行了此代码,它的工作正常,不知道您的问题是什么
int[] GoalIds = new int[7] { 31935,31940, 31976,31993, 31994, 31995, 31990};
var a = string.Join(",", GoalIds);
Console.WriteLine(a);
Console.ReadLine();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.