使用日期范围运行LINQ查询,使用正确的日期格式检查C#

时间:2015-10-21 10:08:10

标签: c# linq asp.net-ajax asp.net-3.5 jqwidget

我在运行LINQ查询时遇到麻烦,我坚信是因为日期格式。我从$ ajax jQuery函数获取ASP.NET C#代码隐藏webMethod中的日期和其他标记的字符串值,我将字符串日期转换为Convert.ToDateTime。使用jQWidget在网页上选择了日期,格式如下面的警报

屏幕截图所示

enter image description here

以及代码隐藏调试的屏幕截图

enter image description here

Ajax jQuery函数

  $("#ctl00_ContentArea_btnFilterStudent").click(function (e) {

            e.preventDefault();

            var givenDateFrom = $("#CldBoxStudentFilter_From").jqxDateTimeInput('getDate');

            var givenDateTo = $("#CldBoxStudentFilter_To").jqxDateTimeInput('getDate');

            if(givenDateFrom && givenDateTo)
            {

                var selectedFilteredValue = $("#<%=ddLStudentFilterList.ClientID%> option:selected").val();

               $.ajax({
                    url: 'TutorForm.aspx/FilterStudentListInRelationToStaffByDateRange',
                    type: "POST",
                    data: JSON.stringify({ GivenStaffID: SelectStaffID, SelectFilterOption: selectedFilteredValue, FromDate: givenDateFrom, ToDate: givenDateTo }),
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (response) {

                        alert("success");
                    },
                    failure: function (response) {
                        alert(response.d);
                    }
                }).done(function (response) {

                });

C#代码

 [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public static string FilterStudentListInRelationToStaffByDateRange(string GivenStaffID, string SelectFilterOption, string FromDate, string ToDate)
    {
        string returnList = string.Empty;

        bool StudentStaffRelationActive = false; 

        List<StudentInRelationToStaffModelView> studentStaffRelationObject = new List<StudentInRelationToStaffModelView>();

        //var queryList = StaffRelay.GetStudentsForRelationship(Convert.ToInt32(GivenStaffID), Convert.ToDateTime(FromDate), Convert.ToDateTime(ToDate), RelationshipStatus.None);


        if (SelectFilterOption.Equals("StudentFilterSelectAll"))
        {

            var queryList = StaffRelay.GetStudentsForRelationship(Convert.ToInt32(GivenStaffID), null, null, RelationshipStatus.None);

            var x = (from b in queryList
                    where b.RelationshipDateStart >= Convert.ToDateTime(FromDate) && b.RelationshipDateEnd <= Convert.ToDateTime(FromDate)
                    select b).ToList();

        //need help here......

1 个答案:

答案 0 :(得分:0)

看起来像是时区问题 - 客户端+1小时调整。您可能必须在服务器端指定期望日期的时区。

    DateTime rawDate = Convert.ToDateTime(matchFromXmlSoccer.Date);
    TimeZoneInfo est = TimeZoneInfo.FindSystemTimeZoneById("GMT Standard Time");
    DateTime targetDateTime = TimeZoneInfo.ConvertTime(rawDate, est);