Delete record using linQ with lambda Expression

asp.net c# entity-framework lambda linq

Question

I want to delete records of sql database using linq lambda expression. What should I do?

Here 'student' is my table and 'db' is object of .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

I got reference from previous suggestion.

studentEntities db_dlt = new studentEntities();
                var students = db_dlt.student.FirstOrDefault(s => s.caurse_id == caurseID);
                if (students != null)
                {
                    db_dlt.student.Remove(students);
                    db_dlt.SaveChanges();
                }
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