[英]Using recursive properties ASP.NET / C#
我正在尋找一種方法來獲取通用結果集,並使用jQuery Dyantree使用ASP.NET MVC將其轉換為樹結構。 我有一個類似的測試課程:
public class GenericResults
{
public string ClaimId { get; set; }
public string DrugName { get; set; }
public int PatientId { get; set; }
//simulates database call, this could be any type of result set
public static List<GenericResults> MockDatabaseCall()
{
return new List<GenericResults>()
{
new GenericResults { ClaimId = "abc", DrugName="Drug 1", PatientId=1 },
new GenericResults { ClaimId = "bcd", DrugName="Drug 2", PatientId=1 },
new GenericResults { ClaimId = "def", DrugName="Drug 3", PatientId=1 },
new GenericResults { ClaimId = "fgi", DrugName="Drug 4", PatientId=1 },
new GenericResults { ClaimId = "ijk", DrugName="Drug 5", PatientId=2 },
new GenericResults { ClaimId = "klm", DrugName="Drug 6", PatientId=2 },
new GenericResults { ClaimId = "mno", DrugName="Drug 7", PatientId=2 },
new GenericResults { ClaimId = "pqr", DrugName="Drug 8", PatientId=2 },
new GenericResults { ClaimId = "qrs", DrugName="Drug 9", PatientId=2 },
new GenericResults { ClaimId = "stu", DrugName="Drug 10", PatientId=2 },
};
}
}
Dynatree所需的數據結構如下:
public string Title { get; set; }
public List<TreeView> Children { get; set; }
public bool IsFolder
{
get
{
return this.Children.Count > 0;
}
}
在這個例子中,我們確實不需要ClaimId
屬性,但是我想重用已經編寫的存儲過程來饋送給樹,所以我想包含它。 我需要將分組后的內容變成其自己的TreeView
對象,並將Children
屬性作為具有分組后的值的所有記錄。 到目前為止, MockDatabaseCallMethod
,如果我對分組PatientId
我需要兩個TreeView
對象:一個用於每個唯一PatientId。 我希望能夠遞歸地執行此操作,但不知道如何執行,因為有些項目將是葉子(沒有子項),這很好。 這是我的嘗試:
public class TreeView
{
public string Title
{
get;
set;
}
public List<TreeView> Children { get; set; }
public bool IsFolder
{
get
{
return this.Children.Count > 0;
}
}
//made void for the time being to limit compilation errors
public static void ListToTree(string displayName,string groupedOn)
{
//seed data
List<GenericResults> results = GenericResults.MockDatabaseCall();
//"group" on the property passed by the user
var query = results.Select(x => x.GetType().GetProperty(groupedOn).GetValue(x,null)).Distinct();
foreach (var i in query)
{
var treeView = new TreeView();
treeView.Title = displayName;
//iterate over results, if object has the property value of what's currently being iterated over
//create a new TreeView object for it
treeView.Children = results.Where(x => x.GetType().GetProperty(groupedOn).GetValue(x, null) == i)
.Select(n => new TreeView
{
Title = displayName,
Children = x <--no idea what to do here
});
}
}
}
理想情況下,我想要一個ToTree
擴展方法,但是首先,我想對此進行一些說明。 謝謝 :)
我不確定我是否正確地理解了您,但是如果我理解了,我將建議以下解決方案:
-)在TreeView
更改屬性public List<TreeView> Children { get; set; }
public List<TreeView> Children { get; set; }
public List<TreeView> Children { get; set; }
public IEnumerable<GenericResults> Children { get; set; }
public IEnumerable<GenericResults> Children { get; set; }
public IEnumerable<GenericResults> Children { get; set; }
。
-)將您的void ListToTree(string displayName
方法替換為以下內容:
public static List<TreeView> ListToTree(string displayName, string groupedOn)
{
//seed data
List<GenericResults> results = GenericResults.MockDatabaseCall();
List<TreeView> trees = new List<TreeView>();
//"group" on the property passed by the user
var query = results.Select(x => x.GetType().GetProperty(groupedOn).GetValue(x, null)).Distinct();
foreach (int i in query)
{
var treeView = new TreeView();
treeView.Title = displayName;
treeView.Children = results.Where(x => ((int)x.GetType().GetProperty(groupedOn).GetValue(x, null)) == i);
trees.Add(treeView);
}
return trees;
}
但是,如果我能正確理解您的信息,我不確定;-)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.