locked
DbFunctions.AddMinute How to use in lambda expression? RRS feed

  • Question

  • Hi

    I need to delete all items in db with have the date in a specific field previus 30 minutes.

    I use this  :

    hT.Delete(t => t.TRA_INPDAT < DbFunctions.AddMinutes(DateTime.Now,-30).Value);

    But don't work.. I never used Dbfunction and don't underdand because don't work.

    You can help me?

    BR

    Tuesday, August 11, 2015 12:24 PM

All replies

  • Try to declare the DateTime object before you issue the query, e.g. (TRA_INPDAT is a DateTime property of your entity class):

                DateTime date = DateTime.Now.AddMinutes(-30);
                context.hT.Delete(t => t.TRA_INPDAT.Equals(date));

    Hope that helps.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    Tuesday, August 11, 2015 12:40 PM
  • Try to declare the DateTime object before you issue the query, e.g. (TRA_INPDAT is a DateTime property of your entity class):

                DateTime date = DateTime.Now.AddMinutes(-30);
                context.hT.Delete(t => t.TRA_INPDAT.Equals(date));

    Hope that helps.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    This don't work

    DateTime date = DateTime.Now.AddMinutes(-30);
             hTd.Delete(t => t.TRA_INPDAT < date);

    This work

    hTd.Delete(t => t.TRA_USRNAM == "HELLO");

    The problem is how it manage the dates .. I don't undestand how to solve.

            
    Tuesday, August 11, 2015 3:10 PM
  • "manage the dates"? What do you mean? Do you get an error or what happens? Please clarify your issue. If the TRA_INPDAT property is a DateTime and the value of the corresponding column in your database is datetime
    or a date (if it is not, then change the type of column!), the following code should remove all entries with a date/time less than 30 minutes ago:

    DateTime date = DateTime.Now.AddMinutes(-30);
    context.hT.Delete(t => t.TRA_INPDAT.Equals(date));


    Please upload a reproducible sample if you want any further help.

    Please also remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    Tuesday, August 11, 2015 4:07 PM
  • The my objetive is delete all items that it have the date lesser than (now - 30)..

    In this case it delete only one not all I need delete all.

    DateTime date = DateTime.Now.AddMinutes(-30);
    context.hT.Delete(t => t.TRA_INPDAT.Equals(date));

    The my db is SqlServer and my filed type is "datatime", when I run my code Visual studio don't raise the exception It don't do anything.

    using (UnitOfWork<IncaContext> unitOfWorkDelete= new UnitOfWork<IncaContext>())
                    {
    
                        var hTd = unitOfWorkDelete.Repository<HIS_TRACE>();
    
                      
                        DateTime date = DateTime.Now.AddMinutes(-30);
                        hTd.Delete(t => t.TRA_INPDAT < date);
    
                        
                        unitOfWorkDelete.Save();
                        LogFile.WriteLog("Write something");
                    }

    I'm using Entity Framework.

    Wednesday, August 12, 2015 8:26 AM
  • Hi cicciuzzo2000,

    According to your description, you'd like to delete multiple records in Ef.

    You could use RemoveRange() method like below.

    hTd.RemoveRange(hTd.Where(t => t.TRA_INPDAT < date));

    Regards,
    Youjun Tang


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, August 27, 2015 3:17 AM
  • Thanks for your reply.

    I tryed but I obtain a timeout from SQL Server... I set in connectionstring the timeout and in contexdb set timeoutcommand but the result is same.

    This code run in a windows service every 30 second ... It could be the reason?

    BR

    Sunday, August 30, 2015 9:32 AM
  • Thanks for your reply.

    I tryed but I obtain a timeout from SQL Server... I set in connectionstring the timeout and in contexdb set timeoutcommand but the result is same.

    This code run in a windows service every 30 second ... It could be the reason?

    BR


    You should be using SQL Server itself to delete the records and not EF doing it by using cascading deletes and/or using a stored procedure to delete the records. 
    Sunday, August 30, 2015 2:44 PM
  • I tryed with this and now work:

    var soglia = DateTime.Now.AddDays(-30); var sqlDelete = "DELETE TABELLA WHERE TRA_INPDAT < @periodo"; hTd.ExecuteProcedure(sqlDelete, new SqlParameter("@periodo", soglia));

    My problem is that my application work with many data, in this case I need to delete 24000 record, and this only one case.

    Why EF not manage big data?

    In this time I'm developing an Repository with Unit Of Work and I'm testing the perfomance of EF..

    What should I consider in order to better manage large volumes of data?

    Thanks

    Monday, August 31, 2015 3:22 PM
  • My problem is that my application work with many data, in this case I need to delete 24000 record, and this only one case.

    Maybe you should consider using a bulk delete.

    https://www.credera.com/blog/technology-insights/microsoft-solutions/entity-framework-batch-operations-using-ef-utilities/

    • Proposed as answer by Fred Bao Monday, September 7, 2015 9:51 AM
    Monday, August 31, 2015 5:36 PM