Delete record using linQ with lambda Expression

asp.net c# entity-framework lambda linq

Question

I want to use a linq lambda expression to delete records from a sql database. What ought I to do?

In this case, "student" is my table, and "db" is an object of the.edmx file.

public bool DeleteRecord(int caurseID)
{
    studentEntities db = new studentEntities();

    int count = db.student.Where(s => s.caurse_id == caurseID).Count();

    if (count != 0)
    {
        //string subQuery = "delete from student where caurse_id=" + caurseID;
        //SqlCommand subCmd = new SqlCommand(subQuery, conn);
        //subCmd.ExecuteNonQuery();
        db.student. : For Delete, what will be goes here???
    }

    return true;
}
1
3
3/18/2015 2:03:57 PM

Accepted Answer

ZZZ_tmp
5
3/18/2015 2:17:45 PM

Popular Answer

public bool DeleteRecord(int caurseID)
{
   studentEntities db = new studentEntities();

   var students = db.student.Where(s => s.caurse_id == caurseID);


   if(students.Any()) 
   {
       db.DeleteAllOnSubmit(students);

       db.SubmitChanges();
   }

   return true;
}


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow