Question:
Hi, I have the following problem. In a sqlserver database, I have an entity with date fields (datetime), I generated 2 new date fields and updated my model from the database. The fields were imported without problems, I add them to the corresponding class, and when doing the query it tells me:
Unable to cast the System.DateTime type? in System.DateTime, There is already an explicit cast
This is how the fields that do work are declared:
public DateTime FechaCreacion {get;set;}
public DateTime FechaModif {get;set;}
And the ones I'm wanting to add:
public DateTime FechaAsign { get; set; }
public DateTime FechaCierre { get; set; }
And my Query:
query = (from q in _conexion.Ticket_Cab where [bla..bla..bla]
select new Ticket {[bla..bla] FechaCreacion = q.tik_fechacreacion,
FechaModif = q.tik_fechamodif, FechaAsign = q.tik_fechaAsign,
FechaCierre = q.tik_fechaCierre}).ToList();
The last two fields (q.tik_fechaAsign , q.tik_fechaCierre )
underlines them in red and throws me the error that I quote above. Anyone know what might be happening?
Answer:
Your problem occurs when assigning a value of type Nullable<DateTime>
with DateTime
, currently, you declare the dates as follows:
public DateTime FechaAsign { get; set; }
public DateTime FechaCierre { get; set; }
DateTime
is a structure that cannot be assigned null
, I think the same thing happens with any structure.
So you must declare the variable as a value capable of receiving null
by value and you can do it in the following way 1 :
Nullable<DateTime> FechaAssign { get; set; }
Nullable<DateTime> FechaCierre { get; set; }
Or in the "pretty" way :
DateTime? FechaAssign { get; set; }
DateTime? FechaCierre { get; set; }
This happens because in the declaration of the table in your database, the table accepts null
for value in these fields.
If you want the fields to be the same as the previous ones, you should try to add the NOT NULL
clause in the definition of the table in your database.
You must bear in mind that having a Nullable<T>
in your class, you do not directly access the internal DateTime
member, but you must use the object's .Value
property.
To access the real value of the DateTime
that you define in the query, you must do something like the following:
DateTime AlgunSitioDondeUsar = FechaAssign.Value; // Accede al DateTime interno.
Not to mention that you need to do a null
check to check the current value of the field.
Another alternative:
Instead of altering the definition of your Ticket
class, you can just do a check on the query and it should work perfectly, except that you will not have null
values in the class, you can use a value like 01/01/1865 00:00:00 AM
to represent a false or null
value.
The query would look like the following:
query = (from q in _conexion.Ticket_Cab
where [bla..bla..bla]
select new Ticket { [bla..bla] FechaCreacion = q.tik_fechacreacion,
FechaModif = q.tik_fechamodif,
FechaAsign = q.tik_fechaAsign ?? DateTime.Parse("01/01/1800"),
FechaCierre = q.tik_fechaCierre ?? DateTime.Parse("01/01/1800") }
).ToList();
Where 01/01/1800
is the date that indicates that your value is false or null
.
1 : You must do using System;
when using this method.