除了這個問題:
我想在 3 級類別中創建一個分層串列。
我的資料有以下幾種方式:
var appCategoryList = new List<AppCategoryDataModel>() {
new AppCategoryDataModel() { AppCategoryId = 4844, ParentappCategoryId = null , AppCategoryName = "ItemResearch", ProtocolId = 5164, ProtocolTitle="ABC: Evaluation" },
new AppCategoryDataModel() { AppCategoryId = 4844, ParentappCategoryId = null, AppCategoryName = "ItemResearch", ProtocolId = null, ProtocolTitle=""},
new AppCategoryDataModel() { AppCategoryId = 4845, ParentappCategoryId = null, AppCategoryName = "ItemHostpital", ProtocolId = null, ProtocolTitle="" },
new AppCategoryDataModel() { AppCategoryId = 4845, ParentappCategoryId = null, AppCategoryName = "ItemHostpital", ProtocolId =5162 , ProtocolTitle="ABC: 3/28/20" },
new AppCategoryDataModel() { AppCategoryId = 4845, ParentappCategoryId = null, AppCategoryName = "ItemHostpital", ProtocolId =5164 , ProtocolTitle="ABC: Evaluation" },
new AppCategoryDataModel() { AppCategoryId = 4845, ParentappCategoryId = null, AppCategoryName = "ItemHostpital", ProtocolId =5165 , ProtocolTitle="ABC: section", },
new AppCategoryDataModel() { AppCategoryId = 4845, ParentappCategoryId = null, AppCategoryName = "ItemHostpital", ProtocolId =5192 , ProtocolTitle="ABC: 3/31/20", },
new AppCategoryDataModel() { AppCategoryId = 4846, ParentappCategoryId = null, AppCategoryName = "Contact information", ProtocolId = null, ProtocolTitle="", },
new AppCategoryDataModel() { AppCategoryId = 4846, ParentappCategoryId = null, AppCategoryName = "Contact information", ProtocolId =5164 , ProtocolTitle="ABC: Evaluation", },
new AppCategoryDataModel() { AppCategoryId = 4852, ParentappCategoryId = null, AppCategoryName = "UP", ProtocolId =null , ProtocolTitle="", },
new AppCategoryDataModel() { AppCategoryId = 4852, ParentappCategoryId = null, AppCategoryName = "UP", ProtocolId = 5164, ProtocolTitle="ABC: Evaluation", },
new AppCategoryDataModel() { AppCategoryId = 5023, ParentappCategoryId = null, AppCategoryName = "Call survival guide", ProtocolId = null, ProtocolTitle="" },
new AppCategoryDataModel() { AppCategoryId = 5023, ParentappCategoryId = null, AppCategoryName = "Call survival guides", ProtocolId =5164 , ProtocolTitle="ABC: Evaluation" },
new AppCategoryDataModel() { AppCategoryId = 5085, ParentappCategoryId = null, AppCategoryName = "OE", ProtocolId =null, ProtocolTitle="" },
new AppCategoryDataModel() { AppCategoryId = 5085, ParentappCategoryId = null, AppCategoryName = "OE", ProtocolId =5164 , ProtocolTitle="ABC: Evaluation" },
new AppCategoryDataModel() { AppCategoryId = 5099, ParentappCategoryId = null, AppCategoryName = "OEM", ProtocolId =null , ProtocolTitle=null },
new AppCategoryDataModel() { AppCategoryId = 5334, ParentappCategoryId = 5085, AppCategoryName = "mmkk update", ProtocolId =null , ProtocolTitle= null },
new AppCategoryDataModel() { AppCategoryId = 5336, ParentappCategoryId = 5085, AppCategoryName = "xdgdrg", ProtocolId =null , ProtocolTitle=null },
new AppCategoryDataModel() { AppCategoryId = 5348, ParentappCategoryId = 5023, AppCategoryName = "test", ProtocolId =null , ProtocolTitle=null },
new AppCategoryDataModel() { AppCategoryId = 5341, ParentappCategoryId = 5023, AppCategoryName = "New Category Level-1", ProtocolId =null , ProtocolTitle= null },
new AppCategoryDataModel() { AppCategoryId = 5349, ParentappCategoryId = 5341, AppCategoryName = "New Category Level-2", ProtocolId =null , ProtocolTitle= null },
new AppCategoryDataModel() { AppCategoryId = 5352, ParentappCategoryId = 5348, AppCategoryName = "New category3", ProtocolId =null , ProtocolTitle= null },
}.OrderBy(ap => ap.AppCategoryName);
AppCategoryDataModel
public class AppCategoryDataModel
{
public int AppCategoryId { get; set; }
public int? ParentappCategoryId { get; set; }
public string AppCategoryName { get; set; }
public int? ProtocolId { get; set; }
public string ProtocolTitle { get; set; }
}
Dictionary<int, FirstLevelCategory> dictCategories = new Dictionary<int, FirstLevelCategory>();
foreach (AppCategoryDataModel category in appCategoryList)
{
if (!dictCategories.ContainsKey(category.AppCategoryId))
{
dictCategories.Add(category.AppCategoryId, new FirstLevelCategory()
{
AppCategoryId = category.AppCategoryId,
AppCategoryName = category.AppCategoryName,
AppCategoryProtocolData = category.ProtocolId != null ?
new List<ProtocolDetails>() {
new ProtocolDetails()
{
ProtocolId = (int)category.ProtocolId,
ProtocolTitle = category.ProtocolTitle
}
} : new List<ProtocolDetails>(),
SubCategory = category.ParentappCategoryId != null
? GetSubCategoryList(appCategoryList, category.AppCategoryId)
: new List<SecondLevelCategory>()
});
}
else
{
if (category.ProtocolId != null)
dictCategories[category.AppCategoryId].AppCategoryProtocolData.Add(new ProtocolDetails()
{
ProtocolId = (int)category.ProtocolId,
ProtocolTitle = category.ProtocolTitle
});
}
}
foreach (var key in dictCategories.Keys.ToList())
{
dictCategories[key].CategoryProtocolCount = dictCategories[key].AppCategoryProtocolData.Count();
}
一級類別
public class FirstLevelCategory
{
public int AppCategoryId { get; set; }
public string AppCategoryName { get; set; }
public int CategoryProtocolCount { get; set; }
public List<SecondLevelCategory> SubCategory { get; set; }
public List<ProtocolDetails> AppCategoryProtocolData { get; set; }
}
public class ProtocolDetails
{
public int ProtocolId { get; set; }
public string ProtocolTitle { get; set; }
}
我的問題是ParentappCategoryId != null應該作為子類別出現的專案,但這也已添加到第一級類別中。我無法正確過濾。
我應該如何防止首先添加具有 ParentAppCategory 的專案。
預期結果:
ItemResearch
ABC: Evaluation
ItemHostpital
ABC: 3/28/20
ABC: Evaluation
ABC: section
ABC: 3/31/20
Contact information
ABC: Evaluation
UP
ABC: Evaluation
OE
ABC: Evaluation
mmkk update --subcategory
xdgdrg --subcategory
Call survival guides
ABC: Evaluation --details
test --subcategory
New category3 --thirdcategory
New Category Level-1 --subcategory
New Category Level-2 -- thirdcategory
uj5u.com熱心網友回復:
我的問題是 ParentappCategoryId != null 應該作為子類別出現的專案,但這也已添加到第一級類別中。我無法正確過濾。
我將此解釋為我只想要ParentappCategoryId == null字典第一級中的專案。
如果是這種情況,應該可以通過過濾應用程式類別串列來解決,然后再遍歷它以將第一級專案添加到您的字典中。
以下是如何使用.Where()System.Linq 命名空間實作此目的的示例:
代替
foreach (AppCategoryDataModel category in appCategoryList)
和
//using System.Linq;
var firstLevelItems = appCategoryList.Where(app => app.ParentappCategoryId == null);
foreach (AppCategoryDataModel category in firstLevelItems)
注意:不需要單獨的變數;我主要考慮使用它來提高可讀性。
您不妨按如下方式實作它:
//using System.Linq;
foreach (AppCategoryDataModel category in appCategoryList.Where(app => app.ParentappCategoryId == null))
另外——我不確定這部分是否會給你帶來問題,但邏輯對我來說看起來很奇怪:
SubCategory = category.ParentappCategoryId != null
? GetSubCategoryList(appCategoryList, category.AppCategoryId)
: new List<SecondLevelCategory>()
我將代碼解釋為:
僅當有父
category物件時才填充SubCategorycategory
對我來說,鑒于您對第一級專案沒有父級的條件,這似乎意味著第一級專案都不應該有任何子類別。我覺得這很矛盾。如果我錯了,請糾正我。
如果我是對的,我建議實施GetSubCategoryList(),如果其中沒有任何專案appCategoryList具有ParentappCategoryId == category.AppCategoryId.
如果你有,你可以簡單地去
SubCategory = GetSubCategoryList(appCategoryList, category.AppCategoryId)
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/468067.html
