簡體   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