Reputation: 281
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);
}
}
Upvotes: 1
Views: 196
Reputation: 5990
Try this
using (StreamWriter file = new StreamWriter(outCsvFile))
{
for (int i = 0; i < pr.Length; ++i)
{
string[] subecjtsIDS = new string[] {""};
subecjtsIDS = db.GetUsersList(pr[i].ProjectID);
foreach(var id in subecjtsIDS)
{
file.WriteLine(pr[i].ProjectID + ',' + pr[i].ProjectTitle + ',' + id );
}
}
}
This will write more rows but if you use string.Join("," subecjtsIDS)
then the number of columns increased and your data will become very complex to understand.
Upvotes: 0
Reputation: 16708
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);
Upvotes: 4
Reputation: 678
You are going to need to loop through that array and create a string where the values are comma-separated (or whatever it is you want as output). For example:
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);
string formattedSubecjtsIDs;
for (int j = 0; j < subecjtsIDs.length; j++)
{
if (!string.IsNullOrEmpty(formattedSubecjtsIDs))
formattedSubecjtsIDs += ", ";
formattedSubecjtsIDs += subecjtsIDs[j];
}
file.WriteLine(pr[i].ProjectID + ',' + pr[i].ProjectTitle + ',' + formattedSubecjtsIDS);
}
}
Upvotes: -1