我正在寻找一种获取通用结果集的方法,并使用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
扩展方法,但首先我想对此进行一些指导。谢谢:)
答案 0 :(得分:0)
我不确定我是否理解正确,但如果我这样做,我会建议以下解决方案:
- )在TreeView
中,将属性public List<TreeView> 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;
}
我不确定,如果我理解正确的话; - )