LINQ,无法构造datetime

时间:2012-02-07 21:20:23

标签: linq asp.net-mvc-3 entity-framework-4 linq-to-entities

我需要构建linq,但是day(日期时间类型,i.AllowedTime.AllowedDate.Day),小时和分钟(int类型)存储方式不同。我试着去做:

        DateTime date = DateTime.Now.Date;
        IQueryable<ITW2012Mobile.Core.DataTable.MeetingModel2> result = null;
        if (status == MeetingStatus.RequestedOfYou)
        {

            result = from i in _dbContext.Meetings
                     where i.UserInvitedID == CurrentUserID && i.MeetingStatus == null && EntityFunctions.TruncateTime(i.AllowedTime.AllowedDate.Day) >= date
                     select new ITW2012Mobile.Core.DataTable.MeetingModel2()
                     {
                         Name = i.UserInviter.FirstName + " " + i.UserInviter.LastName,
                         Company = i.UserInviter.Company,
                         MeetingID = i.MeetingID,
                         Time = EntityFunctions.AddMinutes(EntityFunctions.AddHours(i.AllowedTime.AllowedDate.Day, i.AllowedTime.Hour).Value, i.AllowedTime.Minute).Value,
                         Image = i.UserInviter.ProfileImage,
                         Username = i.UserInviter.aspnet_User.UserName
                     };
        }

但有例外:

  

{&#34;数据的日期函数dateadd不支持datepart小时   输入日期。&#34;}

怎么做?

ADDED类MeetingModel2

public class MeetingModel2
{
    public int MeetingID { get; set; }
    public string Name { get; set; }
    public string Company { get; set; }
    public DateTime Time { get; set; }
    public string Image { get; set; }
    public string Username { get; set; }
}

1 个答案:

答案 0 :(得分:2)

您要查询的数据库中的“时间”字段可能是Date数据类型。您无法执行时间操作,例如向Date类型添加分钟或小时。如果您将该字段更改为DateTime,则可能没问题。