简体   繁体   中英

Compare two sql dates using Linq

I need to compare two dates that are on SQLServer, to see if the user has already logged after 10 seconds of his register on the system, but when I try this query, it gives me a

NotSupportedException [System.NotSupportedException] = {"System.DateTime AddSeconds(Double)"}

Query:

var query = from user in this.Query()
            where (user.LastLoginDate.AddSeconds(-10) <= user.CreateDate) 
            select std;

Well here is really dirty (and probably bad perfomance if you don't have indexes) way to solve your problem but it should work:

var query = from user in this.Query()
            where ((user.LastLoginDate.Date == user.CreateDate.Date
                && user.LastLoginDate.Hour == user.CreateDate.Hour
                && user.LastLoginDate.Minute == user.CreateDate.Minute
                && user.LastLoginDate.Second <= user.CreateDate.Second + 10)
                ||(user.LastLoginDate.Date == user.CreateDate.Date
                && user.LastLoginDate.Hour == user.CreateDate.Hour
                && user.LastLoginDate.Minute + 1 == user.CreateDate.Minute
                && 60 - user.CreateDate.Second + user.LastLoginDate.Second <= 10)) 
            select std;

I think you get the idea.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM