Here i am showing how to use SqlClient.SqlFunctions.DateDiff in Entity Framework to delete some records which are older than 8 hours.
/// <summary>
/// Function Name: DeleteAllOutDatedSession
/// Bishnu Tewary
/// 16-09-2013
/// This function will delete all those records which are older than 8 hours
/// </summary>
/// <param name="objEntities">DEMOEntities needs to be submitted to enable SQL-Transactions including Rollbacks</param>
/// <returns> Result Type</returns>
public DB DeleteAllOutDatedSession(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) >= 8) select p).ToList().ForEach(objEntities.DeleteObject);
objEntities.SaveChanges();
}
Report(DB.OK);
}
catch (Exception ex)
{
this.Report(DB.Error, ex);
}
return Result;
}
/// <summary>
/// Function Name: DeleteAllOutDatedSession
/// Bishnu Tewary
/// 16-09-2013
/// This function will delete all those records which are older than 8 hours
/// </summary>
/// <param name="objEntities">DEMOEntities needs to be submitted to enable SQL-Transactions including Rollbacks</param>
/// <returns> Result Type</returns>
public DB DeleteAllOutDatedSession(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) >= 8) select p).ToList().ForEach(objEntities.DeleteObject);
objEntities.SaveChanges();
}
Report(DB.OK);
}
catch (Exception ex)
{
this.Report(DB.Error, ex);
}
return Result;
}
No comments:
Post a Comment