How to make use SqlClient.SqlFunctions.DateDiff in LINQ with example?

Category > LINQ || Published on : Sunday, April 26, 2015 || Views: 8660 || SqlClient.SqlFunctions.DateDiff in LINQ LINQ Examples SqlClient.SqlFunctions.DateDiff


In some situation, we have to filter or delete or get records whose creation times comes under particular time like 1 hour or two hour. In below example I will going to demostrate how to use SqlClient.SqlFunctions.DateDiff in Entity Framework to delete some records which are older than  2 hours.


        public DB DeleteOldRecords(DEMOEntities objEntities)
        {
            Result = DB.Unknown;
            try
            {
                using (objEntities)
                {
                   (from p in objEntities.tblDEMO.Where(x => System.Data.Objects.SqlClient.SqlFunctions.DateDiff("hour", x.Timestamp, DateTime.Now) >= 2) select p).ToList().ForEach(objEntities.DeleteObject);
                    objEntities.SaveChanges();
                }
                Report(DB.OK);
            }
            catch (Exception ex)
            {
                this.Report(DB.Error, ex);
            }
            return Result;
        }

So, In this tutorial, you have learn, How to make use SqlClient.SqlFunctions.DateDiff in LINQ with example.

Stay Tunned for more articles like this.smiley