简体   繁体   中英

cannot convert type 'diaryEntry' to system.datetime

In my diaryEntry tab;e I have id, siteId and date. I pass a date through and I want the list 'DiaryEntry' to store all the dates that match. I then pass this list through a foreach which should then highlight the dates on screen on a calendar.

 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

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 .

Change to:

foreach (Diary_Entry d in DiaryEntry)

And now for each d , you can access its DateTime members.

  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. You can use linq to do this:

    foreach (DateTime d in DiaryEntry.Select(de=>de.Date))
    {
        Calendar1.SelectedDates.Add(d);//CALENDAR 1 IS WHAT I CALLED IT IN ASPX
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM