如何在使用LINQ一个查询创建类别和清单 [英] How to create category and list in one query with Linq

查看:81
本文介绍了如何在使用LINQ一个查询创建类别和清单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用创造一些像这样的的DataList 和流标记:

I want to create something like this using a DataList and Flow markup:

|-----------------------|
|        Title          |
|-----------------------|
| [x] Title             |
| [x] Title             |
| ...                   |
-------------------------

我有一个表(在LINQ2SQL建模)具有这些领域

int id;
int? parentId;
string title;
Foo Parent;
EntitySet<Foo> Children;

现在,当有一个null父,这意味着它是一个顶级类别,如果家长有一个价值,它的类别列表的一部分。

Now, when there is a null parent, it means it's a top level category, and if the parent has a value, it's part of the category list.

我创建了一个的DataList ,并使用了的LinqDataSource 使用查询,看起来像这样:

I have created a DataList, and used a LinqDataSource with a query that looks like this:

<asp:LinqDataSource ID="LinqDataSource1" runat="server" 
    ContextTypeName="MyNameSpace.FooDataContext" 
    Select="new (Title, Children)" TableName="Foo" 
    Where="ParentID = NULL">
</asp:LinqDataSource>

<asp:DataList ID="FooList" runat="server" DataSourceID="LinqDataSource1" 
  BorderColor="Black" BorderWidth="1px" RepeatLayout="Flow">
  <ItemTemplate>
    <asp:Label ID="TitleLabel" runat="server" Text='<%# Eval("Title") %>' />
    <br />
    <asp:Repeater ID="Repeater1" runat="server" DataSourceID="<%# Eval("Children") %>">
        <ItemTemplate>
            <asp:CheckBox ID="Checks" Text="<%# Eval("Title") %>" runat="server" />
        </ItemTemplate>
    </asp:Repeater>
  </ItemTemplate>
</asp:DataList>

这显然是行不通的。 如何利用儿童集合中的DataList项的中继器?

This obviously doesn't work. How can I utilize the Children collection in a repeater of a DataList item?

推荐答案

我想这与我的模式表和它的作品OK.The诀窍是使用Datalist.See标记below.Note的ItemDataBound事件,我使用其中有教师实体的集合Scool实体

I tried this with my schema tables and it works OK.The trick is to use the ItemDataBound event of the Datalist.See markup below.Note that I am using Scool entity which has a collection of Teacher entities

<asp:DataList ID="DataList1" runat="server" DataKeyField="id" 
    DataSourceID="LinqDataSource1" Width="246px" 
    onitemdatabound="DataList1_ItemDataBound">
    <ItemTemplate>
        name:
        <asp:Label ID="nameLabel" runat="server" Text='<%# Eval("name") %>' />
        <div style="border:solid blue 3px;padding:2px;">
       <asp:Repeater ID="rptteachers" runat="server"  >
       <ItemTemplate>
    <asp:Label ID="kllj" runat="server" Text='<%# Eval("name") %>'  ></asp:Label>
    <asp:Label ID="Label1" runat="server" Text='<%# Eval("quals") %>'  ></asp:Label> 
     </ItemTemplate>
       </asp:Repeater>
       </div>
    </ItemTemplate>
</asp:DataList>

然后添加以下到code的后面。

Then you add the following to the code behind.

protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
    {

        Repeater r = e.Item.FindControl("rptteachers") as Repeater;
        if (r == null) return;
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==        ListItemType.AlternatingItem)
        {
            school sc = e.Item.DataItem as school;
            if (sc == null) return;
            r.DataSource = sc.Teachers;
            r.DataBind();
        }
    }

这篇关于如何在使用LINQ一个查询创建类别和清单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆