繁体   English   中英

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

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM