gpt4 book ai didi

c# - 使用递归属性 ASP.NET/C#

转载 作者:太空宇宙 更新时间:2023-11-03 15:57:56 27 4
gpt4 key购买 nike

我正在寻找一种方法,通过 ASP.NET MVC 使用 jQuery Dyantree 获取通用结果集并将其转换为树结构。我有一个像这样的测试类:

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。我希望能够递归地执行此操作,但不确定如何做,因为有些项目将成为叶子(没有 child ),这很好。这是我的尝试:

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 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;
}

不过我不确定我是否理解正确;-)

关于c# - 使用递归属性 ASP.NET/C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22666309/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com