Click here to Skip to main content
15,881,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.
Posted
Updated 19-Apr-17 20:55pm
v2
Comments
virusstorm 21-May-15 7:03am    
First, you have a lot of tags on this, makes sure you are only adding relevant tags. Second, what is the type of item.CreatedDate?
itsathere 21-May-15 7:31am    
type of item.CreatedDate is DateTime. Actually, without using TimeSpan it may not be possible to get the difference and in linq if i will implement it then coding will be complex that's y i use another static method to get exact difference.
Richard Deeming 21-May-15 8:39am    
If the type is already DateTime, then you don't need to call Convert.ToDateTime at all.
itsathere 21-May-15 8:51am    
I am using Model to insert,update data and model is like below code
public DateTime? CreatedDate { get; set; }

afterward i m using it.
before using Convert.ToDateTime it shows because of it's type DateTime?
Richard Deeming 21-May-15 8:53am    
In that case, use either item.CreatedDate.Value or item.CreatedDate.GetValueOrDefault().

The former will throw an exception if the CreatedDate is null, which is the same behaviour as the Convert.ToDateTime method.

The latter will return DateTime.MinValue if the CreatedDate is null.

On the first look...

Seems you have got only one option:
C#
SqlFunctions.DateDiff("h", item.CreatedDate, DateTime.Now)


For further details, please see: SqlFunctions.DateDiff Method[^]

Optionally, as Sascha Lefevre[^] suggested, you can try something like this:
C#
DeliveryLateTime = (DateTime.Now - item.CreatedDate).Hours,
 
Share this answer
 
I also have same problem

so, i used "DbFunctions".

try this.

https://msdn.microsoft.com/en-US/library/system.data.entity.dbfunctions.diffhours(v=vs.113).aspx[^]
 
Share this answer
 
v2
Do you have return type ?

I just guess and I have solution.
Bro, you can see below,

C#
DeliveryLateTime = DateTime.Now.TimeOfDay.Subtract(item.CreatedDate) 
//If DeliveryLateTime is type as TimeSpan

//OR

DeliveryLateTime = DateTime.Now.TimeOfDay.Subtract(item.CreatedDate).Hours
//If DeliveryLateTime is type as int
 
Share this answer
 
Comments
Dave Kreskowiak 7-Sep-16 8:18am    
This question is over a year old. I doubt the OP is still looking for an answer.

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