Reputation: 157
while doing LINQ I got this Error. "Cannot implicitly convert type 'System.DateTime?' to 'System.DateTime'. An explicit conversion exists (are you missing a cast?)"
I know its because of the data type but Convert is not working is there any other way to do it. here is my code.
{var tvr = from t in ce.tbl_TVRinfo
where t.TVRID == fTVRid
select new TVRDetails
{
TVRID = t.TVRID,
Ename = t.Ename,
Esdw = t.Esdw,
Edob =t.Edob, //this field is causing date conversion error
Epob = t.Epob,
Equalification = t.Equalification,
NIC = t.NIC,
EAddress = t.EAddress
}
return tvr.ToList();
}
Upvotes: 0
Views: 256
Reputation: 11090
You are trying to assign a Nullable DateTime to a standard DateTime. If you are confident that the Nullable DateTime contains a value then you could do:
Edob =t.Edob.Value
Or if you are not sure it has a value:
Edob = t.Edob.HasValue ? t.Edob.Value : SomeOtherValidDateTimeValue
Upvotes: 1
Reputation: 107277
You could do
Edob = (DateTime)t.Edob
but an exception will be raised if t.Edob is null. Better still would be to provide a default datetime if Edob is null. The NULL coalescing operator is useful here.
Edob = t.Edob ?? DateTime.Now // Or DateTime.MinValue, or whatever makes sense in your situation
Upvotes: 0