C# 如何将整数数组转换为逗号分隔的字符串

C# 如何将整数数组转换为逗号分隔的字符串,c#,asp.net,arrays,string,C#,Asp.net,Arrays,String,这是我的带ID的整数数组 GoalIds{int[7]} [0]: 31935 [1]: 31940 [2]: 31976 [3]: 31993 [4]: 31994 [5]: 31995 [6]: 31990 我从这段代码中得到了上面的数组 Array GoalIds = FilteredEmpGoals.Select(x => x.GoalId).Distinct().ToArray(); 我正在尝试将其转换为逗号分隔的字符串

这是我的带ID的整数数组

GoalIds{int[7]}
    [0]: 31935
    [1]: 31940
    [2]: 31976
    [3]: 31993
    [4]: 31994
    [5]: 31995
    [6]: 31990
我从这段代码中得到了上面的数组

Array GoalIds = FilteredEmpGoals.Select(x => x.GoalId).Distinct().ToArray();
我正在尝试将其转换为逗号分隔的字符串,如

31935, 31940, 31976, 31993, 31994, 31995, 31990
为了做到这一点,我尝试了

var result = string.Join(",", GoalIds);
但它给了我
“System.Int32[]”
的结果

请让我更新我在这里犯的错误

参考:我看过了,这个例子在那里运行得很好

更新

参考:正如@paqogomez所建议的

我试图将值存储在数组中,但可能它没有正确处理值。现在我确实更改了生成数组的代码,如下所示

int[] GoalIds = FilteredEmpGoals.Select(x => x.GoalId).Distinct().ToArray();
现在它对我来说工作正常。

我已经用c语言运行了这段代码,而且工作正常。我不知道你有什么问题

int[] GoalIds = new int[7] { 31935,31940, 31976,31993, 31994, 31995, 31990};
var a = string.Join(",", GoalIds);
Console.WriteLine(a);
Console.ReadLine();

在将
GoalIds
声明为
Array
类型时,您不能在
String.Join中运行迭代器

尝试:

正如@JeppeStigNielsen在评论中指出的,这也是有效的,并且消除了
ToArray
调用:

var GoalIds = FilteredEmpGoals.Select(x => x.GoalId).Distinct();

尝试GoalIds{int32[7]}?将
Array GoalIds
更改为
int[]GoalIds
var GoalIds
,并在字符串.Join()中使用“,”而不是“,”。请再次阅读我的问题,我列出了获取数组的位置。它是动态创建的,长度也不知道,因为它与int[]GoalIds…配合得很好,我是用Array制作的。@AmitJoki你的答案在哪里?你也可以放弃
.ToArray()
调用(使用
var
string.Join
具有(通用)重载,该重载采用
IEnumerable
@JeppeStigNielsen,我在上面的评论中提到了这一点,但可能需要对其进行编辑来强调。
var GoalIds = FilteredEmpGoals.Select(x => x.GoalId).Distinct();