简体   繁体   English

输出多个列表的最佳方法是什么?

[英]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. 大约有15K用户。

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. 大约有8-9个列表,这意味着系统中大约有8-9个类别的用户。

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. 我正在考虑一个CSV文件,其中的列将代表这8-9个类别,并且在每个列标题下,我将具有该列表中的用户。

I can write all those lists to a CSV but then they appear one below the other. 我可以将所有这些列表写入CSV,但是它们会在另一个列表的下方出现。 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 Excel和任何其他报告平台都可以将此数据转换为所需的格式-例如

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. 如果您将在Excel之类的格式中使用这些CSV,我相信您现在拥有的格式是理想的。

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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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