none
linq to sql delete problem RRS feed

  • Question

  • I have the following code in a C# 2010 application that uses linq to sql when connecting to a sql server 2008 r2 database.
                   var attplan = from p in attDataContext.At_Pln
                                   where p.At_id.Equals((int)Session["AttD"])
                                   select p;
     
                    attDataContext.At_Pln.DeleteOnSubmit(attplan.FirstOrDefault());
                
                    var att = from c in attDataContext.At
                               where c.At_id.Equals((int)Session["AttD"])                       
                                select c;
     
                   
                    attDataContext.At.DeleteOnSubmit(att.First());
                     attDataContext.SubmitChanges();
     
    My problem is the following line of code:
      attDataContext.At_Pln.DeleteOnSubmit(attplan.FirstOrDefault());
     
      The At_Plan table can contain 1 to 500 rows that are related to the At table.
       I want to use some other statement besides FirstOrDefault(). This statment only deletes one row in the
     attplan table. However, the application will not let me try another commands without obtaining a compile
     error.
       Thus can tell me what other linq statement that I can use that will let me delete all the records in the
     attplan table?
    Wednesday, June 20, 2012 1:25 PM

Answers

  • you need to loop through all the records

    var attplan = from p in attDataContext.At_Pln
                                   where p.At_id.Equals((int)Session["AttD"])
                                   select p;
     

    foreach(var rec in attplan){
                    attDataContext.At_Pln.DeleteOnSubmit(rec); 

    }


    KiranMP

    Wednesday, June 20, 2012 4:55 PM