开发者

LINQ - using a query expression to calculate data time difference

I use EntityFramework 4, LINQ, and C#.

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

With this query I receive an error:

DbArithmeticExpression arguments must have a numeric common type. 

Any idea how to do it in Linq? If Linq does not allow it how to do it in a different way?

Thanks for your time

var queryContentsLocked = from cl in context.Cm开发者_Go百科sContentsLockedBies
                          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!!!
                          };


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)
                          };


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 bind the result in GridView you wish


Try this:

DateTime.UtcNow.Subtract(cl.DateLocked).TotalDays
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜