LinQ查询中的DateTime异常 [英] DateTime Exception in LinQ query

查看:110
本文介绍了LinQ查询中的DateTime异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的ASP项目中存在以下问题:

i使用LinQ来搜索数据,包括日期但是它会抛出异常

代码:

I have the follwing problem in my ASP project :
i use LinQ to Retrive data including date however it throw an exception
the Code:

DateTime? castvar = DateTime.Parse(TextBox1.Text);
       DateTime neededDay;
        //and then check it to give the value of the neededadte for linq query
       if (castvar != null)
       {

           neededDay = castvar.Value;


       }
       else
       {
           neededDay = DateTime.Now;
       }

            var DayReservations = (from r in db.reservations
                                  join p in db.Clints on r.clintId equals p.ClintID
                                  join d in db.Emplyees on r.doctorID equals d.EmpID
                                  where r.apointDateStart.Date == neededDay.Date
                                 && r.apointDateStart.Month == neededDay.Month
                                  && r.apointDateStart.Day == neededDay.Day
                                  && r.doctorID == Convert.ToInt32(DoctorList.SelectedValue)
                                  select new { p.Name, d.EmpName, r.apointDateStart, r.apointEndTime, r.suggestedProcedure, r.comment }).ToList();







例外情况:



输入字符串的格式不正确。将字符串转换为DateTime时,解析字符串以获取将每个变量放入DateTime对象之前的日期。确保您的方法参数格式正确。

在LinQ查询中抛出此异常







在SQL中的Reservation表是:




the Exception :

Input string was not in a correct format.When converting a string to DateTime ,parse the string to take the date before putting each variable into the DateTime object.Make sure your method arguments are in right format.
this exception was thrown in the LinQ query



In the SQL the Reservation table is:

[clintId] = <clintId, int,>
     ,[doctorID] = <doctorID, int,>
     ,[apointDateStart] = <apointDateStart, datetime,>
     ,[apointDateEnd] = <apointDateEnd, datetime,>
     ,[comment] = <comment, nvarchar(max),>
     ,[suggestedProcedure] = <suggestedProcedure, nvarchar(max),>







哪里有问题????? !!!!




where is the problem?????!!!!

推荐答案

DateTime.Parse失败时不返回null;它引发了一个例外。使用DateTime.TryParse。

DateTime.Parse does not return a null on failure; it throws an exception. Use DateTime.TryParse.
DateTime neededDay = DateTime.Today;
DateTime.TryParse(textBox1.Text, out neededDay);





如果r.apointDateStart可以为空,那么你的LINQ表达式应首先测试空值。





If "r.apointDateStart" is nullable, your LINQ expression should test for null values first.

引用:

其中r.apointDateStart .Date == neededDay.Date

&& r.apointDateStart.Month == neededDay.Month

&& r.apointDateStart.Day == neededDay.Day

where r.apointDateStart.Date == neededDay.Date
&& r.apointDateStart.Month == neededDay.Month
&& r.apointDateStart.Day == neededDay.Day





如果日期值相等,则无法测试月和日属性也相同。



If the "Date" values are equal, there is no to test if the "Month" and "Day" properties are also equal.


这篇关于LinQ查询中的DateTime异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆