LINQ - Using a Query Expression to Calculate Data Time Difference

I am using EntityFramework 4, LINQ and C #.

I need display data in a GridView from a query expression using Linq; I need a project as an anonymous type value, calculated on the fly DateTime.UtcNow - cl.DateLocked Note: cl.DateLocked is of type DateTime , and I need to know how many days the difference between the two dates.

With this request, I get an error:

 DbArithmeticExpression arguments must have a numeric common type. 

Any idea how to do this in Linq? If Linq doesn't let him do it differently?

thank you for your time

 var queryContentsLocked = from cl in context.CmsContentsLockedBies join cnt in context.CmsContents on cl.ContentId equals cnt.ContentId join u in context.aspnet_Users on cl.UserId equals u.UserId select new { cl.DateLocked, cnt.ContentId, cnt.Title, u.UserName, u.UserId, TimePan = DateTime.UtcNow - cl.DateLocked // Problem here!!! }; 
+6
source share
3 answers
 var queryContentsLocked = from cl in context.CmsContentsLockedBies join cnt in context.CmsContents on cl.ContentId equals cnt.ContentId join u in context.aspnet_Users on cl.UserId equals u.UserId select new { cl.DateLocked, cnt.ContentId, cnt.Title, u.UserName, u.UserId, Days = SqlFunctions.DateDiff("day", cl.DateLocked, DateTime.UtcNow) }; 
+7
source

Without using any SqlFunction

 var grandTotalWork = (from t in db.AssignmentHandymandetails join ad in db.AssignmentDetails on t.assignment_detail_id equals ad.assignment_detail_id where ad.assignment_id == Convert.ToInt32(Label_assignmentId.Text) select new { startTime = t.started_time.Value.TimeOfDay, endTime = t.end_time.Value.TimeOfDay, TotalWorkTime = (t.started_time.Value.TimeOfDay.Duration() - t.end_time.Value.TimeOfDay.Duration()).Duration()}); 

then you can link the result in the gridview you wish

+2
source

Try the following:

 DateTime.UtcNow.Subtract(cl.DateLocked).TotalDays 
-2
source

Source: https://habr.com/ru/post/892309/


All Articles