C# 我可以只从数据表中输出值的第一个实例吗?

C# 我可以只从数据表中输出值的第一个实例吗?,c#,.net,datatable,C#,.net,Datatable,是否可以只输出存储在数据表中的值的第一个实例 我的示例是this=我有一个datatable,其中每行包含一个月的名称。该表按月份按升序排序。我只想在表中第一次出现月份时输出月份名称-如果有五个条目的月份为一月,则文本“一月”只需输出一次 我正在使用一个中继器-代码如下。仅当月份中的值与前一行中的值不同时,该值才应更改 <asp:Repeater ID="rptEvents" runat="server"> <ItemTemplate> <h5>

是否可以只输出存储在数据表中的值的第一个实例

我的示例是this=我有一个datatable,其中每行包含一个月的名称。该表按月份按升序排序。我只想在表中第一次出现月份时输出月份名称-如果有五个条目的月份为一月,则文本“一月”只需输出一次

我正在使用一个中继器-代码如下。仅当月份中的值与前一行中的值不同时,该值才应更改

<asp:Repeater ID="rptEvents" runat="server">
    <ItemTemplate>
    <h5><%#Eval("Month")%></h5>     
        <p>
            <strong><asp:HyperLink ID="hlEvents" runat="server" Text='<%#Eval("Title") %>' NavigateUrl='<%#Eval("QuickLink") %>'></asp:HyperLink></strong><br />
    <%#Eval("Date") %><br />
    <%#Eval("Description") %>
    </p>
    </ItemTemplate>
</asp:Repeater>




嗯,我不确定,但是在db端做怎么样?
选择最小值(当前月份)、标题、标题中的快速链接、快速链接

更新:
好像我被误解了。使用一种方法怎么样

<h5><%#GetMonthName(Eval("Month"))%></h5>  

我建议有两个中继器。外部将绑定到月份列表,内部将绑定到月份内的所有条目:

<asp:Repeater ID="MonthList" runat="server" OnItemDataBound="MonthList_ItemDataBound">
    <ItemTemplate>
        <h2><%# Container.DataItem %></h2>
        <asp:Repeater ID="ItemList" runat="server">
            <ItemTemplate><%# Eval("Name") %></ItemTemplate>
        </asp:Repeater>
    </ItemTemplate>
</asp:Repeater>

对于此标记,隐藏的代码可能如下所示:

public partial class _Default : System.Web.UI.Page
{
    public class Entry
    {
        public string Month {get;set;}
        public string Name {get;set;}
    }

    List<Entry> entries = new List<Entry>()
    {
        new Entry() { Month = "Jan", Name = "name1"},
        new Entry() { Month = "Jan", Name = "name2"},
        new Entry() { Month = "Feb", Name = "name3"},
        new Entry() { Month = "Aug", Name = "name4"},
        new Entry() { Month = "Dec", Name = "name5"},
        new Entry() { Month = "Dec", Name = "name6"}
    };

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            MonthList.DataSource = entries.GroupBy(x => x.Month).Select(x => x.Key);
            MonthList.DataBind();
        }
    }

    protected void MonthList_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            var itemList = (Repeater)e.Item.FindControl("ItemList");
            var month = (string)e.Item.DataItem;
            itemList.DataSource = entries.Where(x => x.Month == month);
            itemList.DataBind();
        }
    }
}
public分部类\u默认值:System.Web.UI.Page
{
公开课入学
{
公共字符串月份{get;set;}
公共字符串名称{get;set;}
}
列表条目=新列表()
{
新条目(){Month=“Jan”,Name=“name1”},
新条目(){Month=“Jan”,Name=“name2”},
新条目(){Month=“Feb”,Name=“name3”},
新条目(){Month=“Aug”,Name=“name4”},
新条目(){Month=“Dec”,Name=“name5”},
新条目(){Month=“Dec”,Name=“name6”}
};
受保护的无效页面加载(对象发送方、事件参数e)
{
如果(!IsPostBack)
{
MonthList.DataSource=entries.GroupBy(x=>x.Month)。选择(x=>x.Key);
MonthList.DataBind();
}
}
受保护的void MonthList_ItemDataBound(对象发送方,RepeaterItemEventArgs e)
{
如果(e.Item.ItemType==ListItemType.Item | | e.Item.ItemType==ListItemType.AlternatingItem)
{
var itemList=(Repeater)e.Item.FindControl(“itemList”);
var month=(字符串)e.Item.DataItem;
itemList.DataSource=entries.Where(x=>x.Month==Month);
itemList.DataBind();
}
}
}

我使用内存中的示例集合和Linq来转换它。首先,我们按月对数据源进行分组。然后,在MonthList_ItemDataBound中,对于绑定的每个月,我们查找内部中继器,并将其与按当前(DataItem)月份过滤的集合绑定。希望这有帮助。

在从数据库中选择时,您不能进行区分吗?甚至在将数据源提供给该中继器时?用一个示例更新了我的答案。我基本上使用了您的解决方案—而不是在控件中使用方法,而是在填充数据表时直接调用GetMonth。真是一件乐事。@Nathan:很高兴听到这对你有帮助。祝你好运
public partial class _Default : System.Web.UI.Page
{
    public class Entry
    {
        public string Month {get;set;}
        public string Name {get;set;}
    }

    List<Entry> entries = new List<Entry>()
    {
        new Entry() { Month = "Jan", Name = "name1"},
        new Entry() { Month = "Jan", Name = "name2"},
        new Entry() { Month = "Feb", Name = "name3"},
        new Entry() { Month = "Aug", Name = "name4"},
        new Entry() { Month = "Dec", Name = "name5"},
        new Entry() { Month = "Dec", Name = "name6"}
    };

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            MonthList.DataSource = entries.GroupBy(x => x.Month).Select(x => x.Key);
            MonthList.DataBind();
        }
    }

    protected void MonthList_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            var itemList = (Repeater)e.Item.FindControl("ItemList");
            var month = (string)e.Item.DataItem;
            itemList.DataSource = entries.Where(x => x.Month == month);
            itemList.DataBind();
        }
    }
}