LINQ中DATETIME的日期部分

时间:2012-10-11 09:17:09

标签: asp.net-mvc linq

(from sms in db.sms
                where sms.NeedModeration == true && sms.ReplyStatus == "test" && **sms.date.Value == date**
                select new 
                 {
                     SMSID = sms.SmsID,
                     Body = sms.body,
                     Date=sms.date,
                     ReplyStatus = sms.ReplyStatus
                 }).AsEnumerable().Select(x=>new VMSMS{
                     SMSID=x.SMSID,
                     Body=x.Body,
                     Date= x.Date.Value,
                     ReplyStatus = x.ReplyStatus
                 })

鉴于代码正常工作,但我想要匹配的部分只有DATETIME类型的日期部分,如

 sms.date.Value.Date == date.Date

但它无法正常显示以下错误

The specified type member 'Date' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.

那么请你告诉我如何从DateTime中提取日期部分在LINQ中输入

这是短信ViewModel代码....

public class VMSMS
{
    public long SMSID { get; set; }
    public string SMS { get; set; }
    public int? SPOID { get; set; }
    public string Body { get; set; }
    public string EmployeeName { get; set; }
    [IgnoreDataMember]
    public DateTime Date { get; set; }        
    public bool? IsRead { get; set; }
    public string DoctorCode { get; set; }
    public string ReplyStatus { get; set; }
}

2 个答案:

答案 0 :(得分:0)

因为您在Date属性上有IgnoreDataMember,所以它不会映射到您的模型中。

因此,在完成预测后,您的比较需要在Linq-to-Object级别进行。

答案 1 :(得分:0)

感谢大家的帮助。最后我得到了应该做的答案

EntityFunctions.TruncateTime(sms.date.Value) == EntityFunctions.TruncateTime(date)

它会截断从日期时间开始的时间。