如何转换DropDownList项以检查linq查询? [英] How to Convert DropDownList items to check in linq query?
问题描述
我在asp.net中有一个下拉列表控件,其中包含从今天到下一个60天的日期以及另一个下拉列表控件的时间。现在,当用户从下拉列表控件中选择日期时,我调用SelectedItemChanged的方法。我想从该数据库中检查该日期,以从另一个未保存在数据库中的下拉列表中获取时间。问题在于转换我无法将具有日期的下拉列表项转换为datetime,以便我可以检查用户在LINQ查询中选择的值或项。
我尝试过多次认为如下:
DateTime date = Convert.ToDateTime(DropDownListDate.SelectedValue);
var selectedDate = 来自 d in datacontext.Appointment
其中 d.AppointmentDate == date
select d;
我也尝试在会话中存储选定的值,但它没有奏效。我也试过了
DateTime date = DateTime.Parse(DropDownlistDate.SelectedValue);
但是效果并不好。我想转换下拉列表控件中的选定值,以便我可以使用它来检查数据库并获取相应的ans。请帮忙。非常感谢任何帮助。
var selectedDate = 来自 d datacontext.Appointment
其中 d.AppointmentDate。包含( date )
选择 d;
您在评论中已经提到的错误,
Quote:字符串未被识别为有效的DateTime。
表示您传递的字符串到Convert.ToDateTime(参数);
不是有效的dateTime对象。在编写可以转换为DateTime对象的字符串时,您需要考虑很多事情。
字符串的语法,月份和日期顺序, - 或 / 的用法也可能会提高这种错误。这并不意味着您的dateTime不是人类可理解的dateTime,而是意味着.NET框架不了解DateTime的格式。
例如如果你使用,
// 你想写2014年11月12日,看起来公平吗?
DateTime dateTime = new DateTime( 12 , 11 , 2014 );
// 抛出错误
要最大限度地减少此错误,您需要传递参数
// 2014年,11个月和第12天。
DateTime dateTime = new DateTime( 2014 , 11 , 12 );
字符串相同的东西。在从String转换为DateTime时,您需要好好处理DateTime分隔符(/在.NET中使用),月份日序列等。本文档 [ ^ ]解释了这个概念。
传递字符串的示例代码是,
DateTime dateTime = Convert.ToDateTime( 05/01/1996);
// 转换为5/1/1996 12:00:00 AM
这样我已经转换来自DropDownList的日期并且工作正常。
DateTime date = Convert.ToDateTime(DropDownListDate.SelectedValue.ToString());
I have a dropdownlist control in asp.net which contains dates from today to next 60 days and another dropdownlist control for time. Now, when a user selects a date from the dropdownlist control I call a method of SelectedItemChanged. I want to check that date from that database to get the times from another dropdownlist which is not saved in database. The problem is in the conversion I am not able to convert the items of dropdownlist which has dates to datetime so that I can check the value or item which is selected by the user in LINQ query.
I have tried many thinks as below:
DateTime date = Convert.ToDateTime(DropDownListDate.SelectedValue);
var selectedDate = from d in datacontext.Appointment
where d.AppointmentDate == date
select d;
I also tried to store selected value in session but it didn't worked. I also tried the
DateTime date = DateTime.Parse(DropDownlistDate.SelectedValue);
but it didn't worked as well. I want to convert the selected value from the dropdownlist control so that I can use that to check in the database and get the appropriate ans. Please Help. Any help is really appreciated.
var selectedDate = from d in datacontext.Appointment where d.AppointmentDate.Contains( date) select d;
The error that you've already mentioned in the comments,
Quote:String was not recognized as a valid DateTime.
means that the String that you're passing to theConvert.ToDateTime(argument);
is not a valid dateTime object. There are actually many things you need to consider while writing a string that can be converted to a DateTime object.
The syntax of the string, the month and date sequence, usage of - or / might also raise this sort of error. It doesn't mean that your dateTime is not a human understandable dateTime but it means that the .NET framework doesn't understand the format of your DateTime.
For example if you use,
// you wanted to write 12th november 2014, seems fair? DateTime dateTime = new DateTime(12, 11, 2014); // throws error
To minimize this error, you need to be passing the parameters as
// 2014 year, 11 month and 12th day. DateTime dateTime = new DateTime(2014, 11, 12);
Same stuff for the strings. While converting from String to a DateTime, you need to take a good care of the DateTime seperator (/ is used in .NET), the Month year day sequence etc. This document[^] of MSDN explains this concept a little bit accurately.
A sample code for you to pass a string would be,
DateTime dateTime = Convert.ToDateTime("05/01/1996"); // converts to 5/1/1996 12:00:00 AM
Read this[^] article of mine to understand the DateTime in (a little bit) depth.
This way I have converted the date from DropDownList and is working fine.
DateTime date = Convert.ToDateTime(DropDownListDate.SelectedValue.ToString());
这篇关于如何转换DropDownList项以检查linq查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!