简体   繁体   中英

What will be the best way to output multiple lists?

I am preparing a report where I need to categorize each user in the system. There are about 15K users.

So I am going through each user, checking them for various conditions, and based on the conditions assign each user to a list.

There are about 8-9 lists which means there are about 8-9 categories of users in the system.

Now I need to report these users in some easy to understand format. I was thinking about a CSV file where the columns would represent those 8-9 categories and under each column header, I will have the users from that list.

I can write all those lists to a CSV but then they appear one below the other. I don't know how can I write them in a tabular format so that it is easy to read and understand.

Eg let's consider I have three categories of users.

category1: abc, def, ghi
category2: lmn, opq, rst
category3: uvw, xyz, adf

So my output should be like below:

category1      category2      category3
abc              lmn             uvw
def              opq             rst
uvw              xyz             adf

I am open to other suggestions as well regarding how I can output the results in an easy to understand format.

For export data purpose, or for storing in database you can use only one format:

user   category
user1  category1
user1  category2
user2  category1
user2  category2

Excel, and any other reporting platform can pivot this data to required format - for example

user   category1  category2  category3
user1  x          x
user2  x                     x

If you will be using these CSV's in something like Excel I believe the format you have right now is ideal.

For outputting the results, what about something like this:

// Get categories with all users in it
// and get max count in a category across all categories
List<List<Category>> categories = GetCategoriesWithUsers();
int maxUsersInCategory = categories.Max(x => x.Count);

using (StreamWriter writer = new StreamWriter("output.csv")
{
    // Loop through each user per category
    for (int userCount = 0; userCount < maxUseresInCategory; userCount++)
    {
       string outputLine = string.Empty;

       // Loop through each category
       foreach(List<Category> category in categories)
       {
          // If category end isn't reached, add user
          if (category.Length > userCount)
          {
              outputLine += category[userCount];
          }

          outputLine += ",";
       }

       outputLine = outputLine.Remove(outputLine.Length - 1);
       writer.WriteLine(outputLine);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM