Click here to Skip to main content
15,881,938 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In my first query (stringdQuery) I am trying to pass a string date.

C#
var stringQuery = (from p in db.Database_CRE_Events
                   select new Loan()
                   {
                       cDate = p.LastUpdated

                   }).FirstOrDefault();


Then I would like to convert string date from the first query into datetime. This is because, I would use this date in my second query (dateQuery) in where clause which only accepts datetime type field.

C#
if (stringdQuery != null && stringdQuery.cDate.HasValue)
         {
             dtt = Convert.ToDateTime(stringdQuery);

         }


C#
var dateQuery = (from p in db.Database_CRE_Events.Where(c => c.LastUpdated == dtt)
                select new Loan()
                {
                    latest = p.Date

                }).FirstOrDefault();



When I execute this function and on the client-side, I get thrown error – Unable to cast object of type 'STW_V1.Controllers.Loan' to type 'System.IConvertible

Error on the following line:

C#
dtt = Convert.ToDateTime(stringQuery); 


Am I missing something in the code?

Apology in advance, if the above explanation is not clear but please feel free to ask any further questions.

Thank you
Posted
Updated 14-May-15 4:05am
v2

1 solution

There are two problems:

1: stringdQuery is of type Loan. So you're trying to convert a Loan-object to DateTime. But conversion isn't neccessary in the first place - just access the DateTime-value of stringdQuery.cDate by its .Value-property:
C#
dtt = stringdQuery.cDate.Value;


2: I don't know what your intention is here but your two queries don't make much sense to me: You're querying for the first Database_CRE_Event, get its LastUpdated-date and then query for the first Database_CRE_Event whose LastUpdated-date is equal to the former. Which will be the same as from the first query. You could as well select the Date-property in the first place.
 
Share this answer
 
Comments
Maciej Los 14-May-15 10:54am    
+5!
Sascha Lefèvre 14-May-15 11:16am    
Thank you, Maciej :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900