[英]cannot convert type 'diaryEntry' to system.datetime
In my diaryEntry tab;e I have id, siteId and date. 在我的diaryEntry标签中; e我有id,siteId和日期。 I pass a date through and I want the list 'DiaryEntry' to store all the dates that match. 我传递了一个日期,并且希望列表“ DiaryEntry”存储所有匹配的日期。 I then pass this list through a foreach which should then highlight the dates on screen on a calendar. 然后,我通过一个foreach传递此列表,然后该列表应突出显示日历上屏幕上的日期。
DateTime tempDate = System.Convert.ToDateTime(Session["Diary_Date"]);
DateTime startOfMonth = new DateTime(tempDate.Year, tempDate.Month, 1);
DateTime endOfMonth = startOfMonth.AddMonths(1).AddDays(-1);
List<Diary_Entry> DiaryEntry = new List<Diary_Entry>();
DiaryEntry = (from DE in db.Diary_Entries
where DE.Site_Id == 1
&& DE.Date >= startOfMonth && DE.Date <= endOfMonth
select DE).ToList();
foreach (DateTime d in DiaryEntry)//list is name of list you use avoue(in linq)
{
Calendar1.SelectedDates.Add(d);//CALENDAR 1 IS WHAT I CALLED IT IN ASPX
}
ERROR: ecannot convert type 'diaryEntry' to system.datetim 错误:无法将类型“ diaryEntry”转换为system.datetim
Can someone possibly advise me how to resolve this that ye 有人可以建议我如何解决这个问题吗
The issue is here: 问题在这里:
foreach (DateTime d in DiaryEntry)
DiaryEntry
is List<Diary_Entry>
, not a list of DateTime
. DiaryEntry
是List<Diary_Entry>
,而不是DateTime
的列表。
Change to: 改成:
foreach (Diary_Entry d in DiaryEntry)
And now for each d
, you can access its DateTime
members. 现在,对于每个d
,您都可以访问其DateTime
成员。
foreach (var d in DiaryEntry)
{
Calendar1.SelectedDates.Add(d.Date); //assuming d.Date is a datetime
}
Your problem is that you have list of Diary_Entry, not DateTime, so if you want to iterate through datetime enumerable, you should get it. 您的问题是您拥有Diary_Entry的列表,而不是DateTime的列表,因此,如果要遍历可枚举的datetime,则应获取它。 You can use linq to do this: 您可以使用linq执行此操作:
foreach (DateTime d in DiaryEntry.Select(de=>de.Date))
{
Calendar1.SelectedDates.Add(d);//CALENDAR 1 IS WHAT I CALLED IT IN ASPX
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.