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

  LINQ   Sunday, April 26, 2015   4844 | 1 Comment | No Download | No Demo | pawan
 Tags: 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

 

Post Comments

if you have some suggestion, question, request on How to make use SqlClient.SqlFunctions.DateDiff in LINQ with example?.

Please use the comment box to express your views.

 

Comments: