LINQ - 使用查询表达式计算数据时间差

Gib*_*boK 7 linq datetime linq-to-entities

我使用EntityFramework 4,LINQ和C#.

我需要使用Linq从查询表达式在GridView中显示数据; 我需要项目作为anonymous type飞行值计算DateTime.UtcNow - cl.DateLocked注意:cl.DateLocked属于类型DateTime,我需要知道这两个日期之间的差异天数.

使用此查询,我收到一个错误:

DbArithmeticExpression arguments must have a numeric common type. 
Run Code Online (Sandbox Code Playgroud)

知道如何在Linq中做到这一点?如果Linq不允许它以不同的方式做到这一点?

谢谢你的时间

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!!!
                          };
Run Code Online (Sandbox Code Playgroud)

Mag*_*nus 8

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)
                          };
Run Code Online (Sandbox Code Playgroud)