I am trying to build a console application using Web Services. Its use 2 functions. The first one GetAllProject
outputs ptojectsID and ProjectsNames.
The second function is GetUsersList
and it outputs the list of users. Its need as a mandatory parameter the projectID which has been requested by calling the first function.
What I would like to do its to output into a CSV file the projectID, the projectName and the totals of userIDs.
When I run the console, it worked fine but in the column for the totals of userIDs I gets as an output System.String[]
.
I don't know what I could do to instead of extracting on each line System.String[]
the actual total of usersIDs corresponding to each projectID
I don't know how to achieve this. Here you have my code.
string outCsvFile = string.Format(@"C:\\test\\test.csv");
WS.Projects[] pr = db.GetAllProject();
using (StreamWriter file = new StreamWriter(outCsvFile))
{
for (int i = 0; i < pr.Length; ++i)
{
string[] subecjtsIDS = new string[] {""};
subecjtsIDS = db.GetUsersList(pr[i].ProjectID);
file.WriteLine(pr[i].ProjectID + ',' + pr[i].ProjectTitle + ',' + subecjtsIDS);
}
}
If I'm correctly interpreting your requirements, you're not trying to sum the user IDs (which wouldn't make sense), you're just trying to list them as part of a CSV row. Assuming that's correct...
The problem is that this line
file.WriteLine(pr[i].ProjectID + ',' + pr[i].ProjectTitle + ',' + subecjtsIDS);
Is attempting to concatenate a string array onto a string. To do this, .NET will call the array's .ToString()
method, and, for most reference types in the framework, this just prints the name of the type, i.e. "System.String[]".
Instead, you need to iterate the array and print its contents. The String class provides a nice way to do this: the Join()
method:
file.WriteLine(pr[i].ProjectID + ',' + pr[i].ProjectTitle + ',' + String.Join(",", subecjtsIDS));
If, however, you're trying to add the number of subjects associated with each project to each line, you just want the Length
of the array:
file.WriteLine(pr[i].ProjectID + ',' + pr[i].ProjectTitle + ',' + subecjtsIDS.Length);