将linq sql结果放入分层结构,以便在无序列表中使用(对于jquery树)

时间:2021-07-16 08:53:46

I have 5 tables in a L2S Classes dbml : Global >> Categories >> Sub-Category >> Item >> Item Data. I want to be able to navigate from the Global table down a tree like structure to get to the items - displaying the title from the Item Data table.

我在L2S类dbml中有5个表:全局>>类别>>子类别>>项目>>项目数据。我希望能够从Global表中导航到树状结构,以获取项目 - 显示项目数据表中的标题。

I have an existing control that uses a IHierarchyData / IHierarchicalEnumerable extended collection by iterating over the collection to output an un-ordered list which I then turn into a tree with jquery. I did it based on Return Un-Ordered List from hierarchical sql data

我有一个现有的控件,它使用IHierarchyData / IHierarchicalEnumerable扩展集合,迭代集合以输出一个无序列表,然后我将其变成一个带有jquery的树。我是基于来自分层sql数据的Return Un-Ordered List做的

Is there an easy generic way that I could use to put the data from the above table structure into a a Hierarchical structure so that I could re-use my existing control and and just pass in a different collection.

是否有一种简单的通用方法可以将上表结构中的数据放入一个Hierarchical结构中,这样我就可以重用现有的控件并传入不同的集合。

4 个解决方案

#1


Have you tried Nested Listviews? I have implemented this solution in several pages. I use ObjectDataSources instead of LinqDataSources directly to keep my data logic separate, but the nested EntitySets work beautifully.

您是否尝试过嵌套列表视图?我已经在几页中实现了这个解决方案。我直接使用ObjectDataSources而不是LinqDataSources来保持我的数据逻辑分离,但嵌套的EntitySets工作得很漂亮。

#2


I think what is hanging you up is that you don't need four tables to do this.

我认为让你烦恼的是你不需要四张桌来做这件事。

Look at Return unordered list from hierarchical sql data again. There aren't four tables there. There's only one.

再次从分层sql数据中查看Return无序列表。那里没有四张桌子。只有一个。

#3


You can use the Union operator to mash your four tables together. http://weblogs.asp.net/zeeshanhirani/archive/2008/04/11/union-operator-part-12.aspx

您可以使用Union运算符将四个表混合在一起。 http://weblogs.asp.net/zeeshanhirani/archive/2008/04/11/union-operator-part-12.aspx

#4


Since you are dealing different types in this case, you will have to implement a common interface on each of them to make the code generic. The basic approach would be to create an interface that includes any needed properties (e.g. DisplayText, ActionURL, etc) then iterate over the collection recursively.

由于在这种情况下处理不同类型,因此必须在每个类型上实现通用接口以使代码通用。基本方法是创建一个包含任何所需属性的接口(例如DisplayText,ActionURL等),然后递归迭代该集合。

Here's a rough example:

这是一个粗略的例子:

 public interface IDataItem
    {
        string DisplayText { get; }
        string ActionUrl { get;  }
        bool HasChildren { get; }
        IEnumerable<IDataItem> GetChildren();
    }

 public void CreateTree(HtmlTextWriter writer, IEnumerable<IDataItem> collection)
    {
        writer.WriteFullBeginTag("ul");
        foreach (var data in collection)
        {
            writer.WriteFullBeginTag("li");
            writer.WriteBeginTag("a");
            writer.WriteAttribute("href",data.ActionUrl);
            writer.Write(HtmlTextWriter.TagRightChar);
            writer.Write(data.DisplayText);
            writer.WriteEndTag("a");
            if(data.HasChildren)
                CreateTree(writer, data.GetChildren());
            writer.WriteEndTag("li");
        }
        writer.WriteEndTag("ul");
    }

You will have to implement the interface on each of your types that you want included in the treeview. The just pass in the collection of the top level type and the method above will walk down the hierarchy creating the needed nested list.

您必须在树视图中包含的每个类型上实现接口。*类型和上面方法的集合中的正确传递将沿着层次结构向下遍历,从而创建所需的嵌套列表。

#1


Have you tried Nested Listviews? I have implemented this solution in several pages. I use ObjectDataSources instead of LinqDataSources directly to keep my data logic separate, but the nested EntitySets work beautifully.

您是否尝试过嵌套列表视图?我已经在几页中实现了这个解决方案。我直接使用ObjectDataSources而不是LinqDataSources来保持我的数据逻辑分离,但嵌套的EntitySets工作得很漂亮。

#2


I think what is hanging you up is that you don't need four tables to do this.

我认为让你烦恼的是你不需要四张桌来做这件事。

Look at Return unordered list from hierarchical sql data again. There aren't four tables there. There's only one.

再次从分层sql数据中查看Return无序列表。那里没有四张桌子。只有一个。

#3


You can use the Union operator to mash your four tables together. http://weblogs.asp.net/zeeshanhirani/archive/2008/04/11/union-operator-part-12.aspx

您可以使用Union运算符将四个表混合在一起。 http://weblogs.asp.net/zeeshanhirani/archive/2008/04/11/union-operator-part-12.aspx

#4


Since you are dealing different types in this case, you will have to implement a common interface on each of them to make the code generic. The basic approach would be to create an interface that includes any needed properties (e.g. DisplayText, ActionURL, etc) then iterate over the collection recursively.

由于在这种情况下处理不同类型,因此必须在每个类型上实现通用接口以使代码通用。基本方法是创建一个包含任何所需属性的接口(例如DisplayText,ActionURL等),然后递归迭代该集合。

Here's a rough example:

这是一个粗略的例子:

 public interface IDataItem
    {
        string DisplayText { get; }
        string ActionUrl { get;  }
        bool HasChildren { get; }
        IEnumerable<IDataItem> GetChildren();
    }

 public void CreateTree(HtmlTextWriter writer, IEnumerable<IDataItem> collection)
    {
        writer.WriteFullBeginTag("ul");
        foreach (var data in collection)
        {
            writer.WriteFullBeginTag("li");
            writer.WriteBeginTag("a");
            writer.WriteAttribute("href",data.ActionUrl);
            writer.Write(HtmlTextWriter.TagRightChar);
            writer.Write(data.DisplayText);
            writer.WriteEndTag("a");
            if(data.HasChildren)
                CreateTree(writer, data.GetChildren());
            writer.WriteEndTag("li");
        }
        writer.WriteEndTag("ul");
    }

You will have to implement the interface on each of your types that you want included in the treeview. The just pass in the collection of the top level type and the method above will walk down the hierarchy creating the needed nested list.

您必须在树视图中包含的每个类型上实现接口。*类型和上面方法的集合中的正确传递将沿着层次结构向下遍历,从而创建所需的嵌套列表。