简体   繁体   中英

convert Request.QueryString toDatetime and then fetch using datacontext in linq in c# asp.net

I wanna get the Timedate value from another page using request.querystring and then use it an query to compare and pull up the matching datas. The function for the query in linq is:

   protected void User_Querytime()
    {
    DataClasses2DataContext dc1 = new DataClasses2DataContext();
    String  Data = Request.QueryString["TimeOfMessage"];

                var query7 = from u in dc1.syncback_logs
                             where u.TimeOfMessage = Data
                             orderby u.TimeOfMessage descending
                             select u;
                GridView1.DataSource = query7;
                GridView1.DataBind();
    }

Here the "Request.QueryString["TimeOfMessage"]" which i get is DateTime (ex:8/25/2008 9:07:19 AM). I wanted to compare it against the "u.TimeOfMessage" in database and pull up the matching records.

When I use todatetime function to convert from string to datetime ,the value returned is bool and hence not able to compare it against the "Timeofmessage" which is datetime format in database. Can anyone help me in this?

Do you mean Convert.ToDateTime? This returns DateTime (not bool). Do you mean DateTime.TryParse? Simply use any of:

DateTime when = DateTime.Parse(data);
DateTime when = DateTime.ParseExact(data);
DateTime when = Convert.ToDateTime(data);

Then use "when" in the query. I'm not sure the purpose of ordering by it if you know they are all equal, though... did I miss something?

If the issue is that you want only the time part (not the date part), could you clarify that?

the TryParse indeed results a bool (as the success of the parsing):

Dim DateText = Request.QueryString("date")
Dim MyDate As DateTime = Nothing
If DateTime.TryParse(DateText, MyDate) Then
  '--Date was passed correctly
End If

regards Christoph

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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