how to get TimeSpan of Inserted date in database and DateTime.Now in Hours.
I have linq
var getFacility = (from item in objDB.LabDetails
select new LabDetailsModel
{
DetailsID = item.DetailsID,
CallerName = item.CallerName,
FaciltityID = item.FaciltityID,
FacilityName=(from fac in objDB.Facilities.Where(a=>a.FacilityID==item.FaciltityID) select fac.FacilityName).FirstOrDefault(),
ReqBarCode = item.ReqBarCode,
SpecimenType = item.SpecimenType,
Contact = (from fac in objDB.Facilities.Where(a => a.FacilityID == item.FaciltityID) select fac.Contact).FirstOrDefault(),
CourierName=(from fac in objDB.Facilities.Where(a=>a.FacilityID==item.FaciltityID) select fac.CourierName).FirstOrDefault(),
RequiredTesting = item.RequiredTesting,
RequiredTestingStat = item.RequiredTestingStat,
ConfirmationCode = item.ConfirmationCode,
Representative = item.Representative,
DeliveryType=item.DeliveryType,
DeliveryTypeName = item.DeliveryType == 1 ? "1 hour" : item.DeliveryType == 2 ? "2 hour" : item.DeliveryType == 3 ? "OverNight" : item.DeliveryType == 4 ? "Ground" : item.DeliveryType == 5?"2 Day":"None",
Reference = item.Reference,
Notes = item.Notes,
OrderingPhysicianType = item.OrderingPhysicianType,
PhysicianName = (from fac in objDB.Facilities.Where(a => a.FacilityID == item.FaciltityID) select fac.PhysicianName).FirstOrDefault(),
CreatedDate = item.CreatedDate.Value,
ReceiveDate=(from recv in objDB.ReceiveSpecimen.Where(a=>a.LabDetailsID==item.DetailsID) select recv.ReceiveDate).FirstOrDefault(),
ReceivedBy = (from recv in objDB.ReceiveSpecimen.Where(a => a.LabDetailsID == item.DetailsID) select recv.ReceivedBy).FirstOrDefault(),
ReceiveNote = (from recv in objDB.ReceiveSpecimen.Where(a => a.LabDetailsID == item.DetailsID) select recv.ReceiveNote).FirstOrDefault(),
DeliveryLateTime = (DateTime.Now - Convert.ToDateTime(item.CreatedDate)).Hours,
}).OrderByDescending(a=>a.DetailsID).ToList();
return getFacility;
It shows the error "LINQ to Entities does not recognize the method 'System.DateTime ToDateTime(System.Object)' method, and this method cannot be translated into a store expression." I think error is due to
Convert.ToDateTime(item.CreatedDate)
but unable to find the exact solution after many searches.
Thanks.