C# 使用Linq和group by的数据绑定中继器

C# 使用Linq和group by的数据绑定中继器,c#,asp.net,linq,.net-4.0,C#,Asp.net,Linq,.net 4.0,我需要使用分层数据绑定中继器,如下所示: Category1 - Item1 - Item2 Category2 - Item3 - Item4 我目前有一个数据集,其中包含项目以及每个项目所属的类别 我正在尝试学习Linq,想知道是否有一种方法可以使用Linq实现同样的功能 以下是我尝试过的: var groupbyfilter = from dr in dtListing.AsEnumerable() grou

我需要使用分层数据绑定中继器,如下所示:

Category1
   - Item1
   - Item2
Category2
   - Item3
   - Item4
我目前有一个数据集,其中包含项目以及每个项目所属的类别

我正在尝试学习Linq,想知道是否有一种方法可以使用Linq实现同样的功能

以下是我尝试过的:

var groupbyfilter = from dr in dtListing.AsEnumerable()
                            group dr by dr["Category"];
        DataTable dtFinal = dtListing.Clone();

  foreach (var x in groupbyfilter)
          x.CopyToDataTable(dtFinal, LoadOption.OverwriteChanges);


  rptList.DataSource = dtFinal;
  rptList.DataBind();

但问题是它会重复每个项目的类别。

您需要一个嵌套在另一个项目中的重复项

仅选择类别字段,在
dtlisting
上执行一次独特操作。将其绑定到外部中继器

在第二个中继器中,选择其where条件具有类别字段的数据,该字段等于要数据绑定到中继器项的值。您必须在中继器的
onitem\u数据绑定
事件中处理此问题

这里有一个例子

<%@ Import  Namespace="System.Data" %>
      <asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound">
          <ItemTemplate>
              <div>
                  Category: <b><%# Container.DataItem%></b>
                  <asp:Repeater ID="Repeater2" runat="server">
                      <FooterTemplate>
                          <%="</ul>" %>
                      </FooterTemplate>
                      <HeaderTemplate>
                          <%= "<ul>"%>
                      </HeaderTemplate>
                      <ItemTemplate>
                          <li>
                              <%# ((Data.DataRow)Container.DataItem)[1] %>, <%#  ((Data.DataRow)Container.DataItem)[0] %>
                          </li>
                      </ItemTemplate>
                  </asp:Repeater>
              </div>                
          </ItemTemplate>
      </asp:Repeater>

您可以使用DeoWalk所说的嵌套转发器,或者指定一个分组字段,并在ItemTemplate中设置一个组头,该组头在分组字段每次更改时动态隐藏(或呈现)


fernan在

上的回答中提供了更多详细信息,请检查这可能对您有所帮助:谢谢Pranay,但我正在尝试使用Linq。您想要输出什么还不清楚。包括项目模板或您要查找的内容可能会有所帮助。例如,你想要的东西与仅仅按类别排序而不是按类别分组有什么不同?如果我只是按类别排序,它会重复多次类别。我需要的是类别标题,然后是出现在该类别中的项目。然后是下一个类别标题,后面是该类别中的项目。希望这更有意义。谢谢!正是我需要的。我刚刚对代码进行了一些小的编辑,以使其正常工作。
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;

public class _Default : System.Web.UI.Page
{
    DataTable csvData;
    protected void Page_Load(object sender, System.EventArgs e)
    {
        csvData = Utils.csvToDataTable("data.csv", true);
        GridView1.DataSource = csvData;
        GridView1.DataBind();

        Repeater1.DataSource =
            (from x in csvData.AsEnumerable() select x["category"]).Distinct();
        Repeater1.DataBind();
    }

    protected void Repeater1_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item |
                e.Item.ItemType == ListItemType.AlternatingItem) {
            Repeater rptr = (Repeater)e.Item.FindControl("Repeater2");
            rptr.DataSource =
                csvData.AsEnumerable().Where(x => x["category"].Equals(e.Item.DataItem));
            rptr.DataBind();
        }
    }       
}