简体   繁体   English

Linq到实体并查询日期

[英]Linq-To-Entities and query with dates

I am using the Fullcalendar jquery plugin to show events on a calendar view in my MVC 4 application. 我正在使用Fullcalendar jQuery插件在MVC 4应用程序的日历视图中显示事件。

I have configured the plugin which is correctly calling my controller action passing in two double parameters who represents the start date and the end date showed on the current view in unix timestamp formats. 我已经配置了插件,该插件正确调用了我的控制器操作,并传入了两个双精度参数,它们以UNIX时间戳格式显示了当前视图上显示的开始日期和结束日期。

Here you can see my controller action method 在这里您可以看到我的控制器动作方法

[HttpPost]
public ActionResult GetEvents( double start, double end ) {
    DateTime dtStart = DateTimeUtils.UnixTimeStampToDateTime( start );
    DateTime dtEnd = DateTimeUtils.UnixTimeStampToDateTime( end );

    IPolicyRepository _pRepo = _uow.PolicyRepository;
    IEnumerable<Policy> model = 
    _pRepo.Find( p => p.EndDate >= dtStart.Date && p.EndDate < dtEnd.Date );

    ...
}

The method DateTimeUtils.UnixTimeStampToDateTime(double) converts a unix timestamp in a datetime object in the following way 方法DateTimeUtils.UnixTimeStampToDateTime(double)通过以下方式在datetime对象中转换unix时间戳

public static DateTime UnixTimeStampToDateTime( double unixTimeStamp ) {
    // Unix timestamp is seconds past epoch
    DateTime dtDateTime = new DateTime( 1970, 1, 1, 0, 0, 0, 0 );
    dtDateTime = dtDateTime.AddSeconds( unixTimeStamp ).ToLocalTime();
    return dtDateTime;
}

However the Find method of the repository, which in turn simply pass the lambda to the EF does not return any row even if some rows exists. 但是,仅将lambda传递给EF的存储库的Find方法不会返回任何行,即使存在某些行。 I am suspecting that this problem could be due to differents formats and/or locale I have between the browser and the database server. 我怀疑此问题可能是由于浏览器和数据库服务器之间的格式和/或语言环境不同所致。

How can I solve this problem and create a query which will be aware of locales and specific formats used? 如何解决此问题并创建一个查询,该查询将了解所使用的语言环境和特定格式?

EDIT : For further informations I am adding the model definition here 编辑 :有关更多信息,我在这里添加模型定义

public partial class Policy
{
    public int PolicyID { get; set; }

    [...]

    [Required]
    [DisplayName( "Begin date" )]
    [DisplayFormat( ApplyFormatInEditMode = true, DataFormatString = "{0:dd-MM-yyyy}" )]
    public DateTime BeginDate { get; set; }

    [Required]
    [DisplayName( "Due Date" )]
    [DisplayFormat( ApplyFormatInEditMode = true, DataFormatString = "{0:dd-MM-yyyy}" )]
    public DateTime EndDate { get; set; }
}

I have found the problem. 我发现了问题。

The presence of the attribute DisplayFormat on the model class was causing the issue 模型类上存在属性DisplayFormat导致了问题

[DisplayFormat( ApplyFormatInEditMode = true, DataFormatString = "{0:dd-MM-yyyy}" )]

Simply removed and it's working perfectly! 只需将其删除,即可正常运行!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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