none
Linq2Sql -how do I delete records on certain days? RRS feed

  • Question

  • I have a string array of days like [1,2,3,6,7,9,25] and I have a table with columns that have a 'from date' and 'to date'.
    How can I use a LinQ to SQL statement to delete these days?

    Do,  I have to create a foreach loop and delete each day individually? or is there a way to do a linq2sql statement to batch delete the records on these days?

    Tuesday, November 17, 2009 12:16 AM

Answers

  • Try the method DeleteAllOnSubmit() of the DataContext object .. which does not force you to loop through

    sample code perform the deletion of records which fall on the criteria that fromdate between 3 and 8 from DateSamples table


    using

     

    (NorthwindDataContext db = new NorthwindDataContext())

    {

     

    var qx = db.DateSamples.

    Where(d1 => d1.FromDate >= 3 && d1.FromDate < 8).AsEnumerable()

    ;

    db.DateSamples.DeleteAllOnSubmit(qx);

    db.SubmitChanges();
    }


    hope this helps you

    Thanks
    Jbasingh

    • Marked as answer by Yichun_Feng Monday, November 23, 2009 2:14 AM
    Tuesday, November 17, 2009 7:25 AM