Asp.net foreach语句不能对类型为';日记条目';

Asp.net foreach语句不能对类型为';日记条目';,asp.net,datetime,Asp.net,Datetime,通过foreach循环传入日期列表“DiaryEntry”。它记录的每个日期都将在日历上突出显示 foreach (DateTime d in DiaryEntry) { Calendar1.SelectedDates.Add(d);//CALENDAR1 being the ID of the calendar on the aspx page } 错误:foreach语句无法对“Diary\u Entry”类型

通过foreach循环传入日期列表“DiaryEntry”。它记录的每个日期都将在日历上突出显示

foreach (DateTime d in DiaryEntry)
            {
                Calendar1.SelectedDates.Add(d);//CALENDAR1 being the ID of the calendar on the aspx page
            }
错误:foreach语句无法对“Diary\u Entry”类型的变量进行操作,因为“Diary\u Entry”不包含“GetEnumerator”的公共定义

有人知道我怎么解决这个问题吗?
谢谢

您的日记条目对象需要实现或

发件人:

foreach语句为每个对象重复一组嵌入语句 元素,该元素位于实现 System.Collections.IEnumerable或 System.Collections.Generic.IEnumerable接口

我猜你的意思是有一个数组或其他一些对象的集合。在这种情况下,可以使用foreach对集合进行迭代。另一种可能性是,日记条目上有一个集合属性,您的意思是对它进行迭代。。。也许像下面这样

foreach(DateTime date in DiaryEntry.Dates) 
{
    ...
}

Diary\u Entry
是指某种列表吗?它是基于一个集合类,实现
System.Collections.IEnumerable
还是
System.Collections.Generic.IEnumerable
?如果没有,如果它只有两个表示日期的属性,则不能对其使用
foreach
。您必须分别处理每个属性

Calendar1.SelectedDates.Add(Diary_Entry.Date1);
Calendar1.SelectedDates.Add(Diary_Entry.Date2);
// etc.
如果它有两个日期,表示开始日期和结束日期,并且您希望在该范围内迭代,您将无法对其使用
foreach
,但仍然可以对其使用

for(DateTime d = Diary_Entry.StartDate;d<=Diary_Entry.EndDate;d=d.AddDays(1))
{
   Calendar1.SelectedDates.Add(d);
}

for(DateTime d=Diary\u Entry.StartDate;d)如果不了解更多类型
Diary\u Entry
很难提出好的建议。你能给我们看看它的代码吗?