繁体   English   中英

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

[英]What will be the best way to output multiple lists?

我正在准备一份报告,需要在其中对系统中的每个用户进行分类。 大约有15K用户。

因此,我要遍历每个用户,检查他们的各种条件,并根据条件将每个用户分配给一个列表。

大约有8-9个列表,这意味着系统中大约有8-9个类别的用户。

现在,我需要以一种易于理解的格式报告这些用户。 我正在考虑一个CSV文件,其中的列将代表这8-9个类别,并且在每个列标题下,我将具有该列表中的用户。

我可以将所有这些列表写入CSV,但是它们会在另一个列表的下方出现。 我不知道如何以表格格式编写它们,以使其易于阅读和理解。

例如,让我们考虑一下我有三类用户。

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

所以我的输出应该如下所示:

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

对于如何以一种易于理解的格式输出结果,我也欢迎其他建议。

为了导出数据或存储在数据库中,您只能使用一种格式:

user   category
user1  category1
user1  category2
user2  category1
user2  category2

Excel和任何其他报告平台都可以将此数据转换为所需的格式-例如

user   category1  category2  category3
user1  x          x
user2  x                     x

如果您将在Excel之类的格式中使用这些CSV,我相信您现在拥有的格式是理想的。

为了输出结果,该如何处理:

// 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