简体   繁体   English

无法将类型'diaryEntry'转换为system.datetime

[英]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 . DiaryEntryList<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.

相关问题 无法隐式转换类型&#39;System.Collections.Generic.IEnumerable <System.DateTime> &#39;到&#39;System.DateTime&#39; - Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<System.DateTime>' to 'System.DateTime' 错误返回时无法将类型&#39;string&#39;隐式转换为&#39;System.DateTime&#39; - Error Cannot implicitly convert type 'string' to 'System.DateTime' on return 无法将参数“日期”的值转换为类型“ System.DateTime”。 - Cannot convert the value of parameter 'Date' to the type 'System.DateTime'.' 无法将&#39;string&#39;转换为&#39;System.DateTime&#39; - Cannot Convert 'string' to 'System.DateTime' 无法将类型“System.Data.DataSet”隐式转换为“System.DateTime” - Cannot implicitly convert type 'System.Data.DataSet' to 'System.DateTime' 无法将System.String转换为System.DateTime - Cannot convert System.String to System.DateTime 无法将类型“ System.DateTime”隐式转换为“ LearnScan.LearnUser.NullableDateTime” - Cannot implicitly convert type “System.DateTime” to “LearnScan.LearnUser.NullableDateTime” 运算符“ ??” 不能应用于类型为&#39;System.DateTime&#39;的操作数 - Operator '??' cannot be applied to operands of type 'System.DateTime' 错误:的最佳重载方法匹配具有一些无效参数,并且无法将类型&#39;string&#39;隐式转换为&#39;System.DateTime&#39; - Error: The best overloaded method match for has some invalid arguments and Cannot implicitly convert type 'string' to 'System.DateTime' System.DateTime? vs System.DateTime - System.DateTime? vs System.DateTime
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM