C# ASP.net For循环问题

C# ASP.net For循环问题,c#,asp.net,visual-studio-2010,loops,for-loop,C#,Asp.net,Visual Studio 2010,Loops,For Loop,我有一个For循环的问题,它看起来像这样 for (int i = 0; i < dt.Rows.Count; i++) //for (int i = 0; i < System.Math.Min(dt.Rows.Count, 3); i++) { string date = Convert.ToDateTime(dt.Rows[i]["date"]).ToShortDateString(); sched

我有一个For循环的问题,它看起来像这样

for (int i = 0; i < dt.Rows.Count; i++)
        //for (int i = 0; i < System.Math.Min(dt.Rows.Count, 3); i++)
        {
            string date = Convert.ToDateTime(dt.Rows[i]["date"]).ToShortDateString();
            schedule[date] = (schedule[date] != null ? schedule[date].ToString() : "")    + Server.HtmlEncode(dt.Rows[i]["todo"].ToString()) + "<br />" + dt.Rows[i]["time"].ToString() + "<br />";
        }
        return schedule;
for(int i=0;i”+dt.Rows[i][“time”].ToString()+“
”; } 返回时间表;
这是我用asp.net日历控件制作的日历。 它将所有记录循环到我的数据库中,并返回我的日程安排(该日期的记录),但我不想显示超过3行的文本,您可以添加到日历中(数据库记录)。你知道我怎么做吗


谢谢你,亲切的问候,设计师

你可以对每个日期添加的行数进行计数:

Dictionary<string, int> schedulesDateCount = new Dictionary<string, int>();

for (int i = 0; i < dt.Rows.Count; i++)
{
    string date = Convert.ToDateTime(dt.Rows[i]["date"]).ToShortDateString();

    if(!schedulesDateCount.ContainsKey(date))
        schedulesDateCount[date] = 0;

    if(schedulesDateCount[date] < 3)
    {
        schedule[date] = (schedule[date] != null ? schedule[date].ToString() : "")    + Server.HtmlEncode(dt.Rows[i]["todo"].ToString()) + "<br />" + dt.Rows[i]["time"].ToString() + "<br />";
        schedulesDateCount[date] = schedulesDateCount[date] + 1;
    }
}
return schedule;
Dictionary schedulesDateCount=new Dictionary();
对于(int i=0;i”+dt.Rows[i][“time”].ToString()+“
”; schedulesDateCount[日期]=schedulesDateCount[日期]+1; } } 返回时间表;
注释掉的代码有什么问题?如果我使用该代码,我只从数据库中得到3条记录,所以它只给我3个月的时间:P,所以它只在我的整个日历中显示3个日期,我想要的是在一个日历日内每天不超过3条记录。ty GeorgeSorry为我这个蹩脚的英国荷兰人写了一封信:)这种问题通常最好通过SQL查询直接从数据库中过滤出正确的记录来解决。这也是ty Anders Abel的一种方法,但我希望它保持简单:)@George Duckett我想你应该用如果(!schedulesDateCount.HasKey(date))schedulesDateCount[date]=0,则
.ContainsKey
方法;将是:如果(!schedulesDateCount.ContainsKey(date))schedulesDateCount[date]=0;那它就应该起作用了!