I have a question about LINQ delete. I have 2 tables in the database and they are joined.
First Table: empid
, empname
, salary
, dname
Second Table: dId
, dname
I want to delete one employee from the first table and all references from the child table also. I wrote the following code for that, tables remain in the same state after the query executed. How can I delete it by using LINQ? Please explain what changes are needed to get the right solutions.
public string DeleteData(int id)
{
try
{
var emplist = new List<ViewDetails>();
// tble_dept dept = new tble_dept();
//tble_Emp emp = new tble_Emp();
OperationDataDataContext dc = new OperationDataDataContext();
var employess = from emps in dc.tble_Emps
join depts in dc.tble_depts
on emps.deptid equals depts.id
select new ViewDetails
{
empid = emps.EmpId,
empname = emps.EmpName,
salary = emps.EmpSalary,
dname = depts.Dname,
};
var company = employess.FirstOrDefault(c => c.empid == id);
emplist = employess.ToList();
if (company == null)
return "Company cannot be found";
emplist.Remove(company);
dc.SubmitChanges();
return "ok";
}
catch (Exception ex)
{
return ex.Message;
}
}
public string DeleteData(int id)
{
try
{
var emplist = new List<ViewDetails>();
// tble_dept dept = new tble_dept();
//tble_Emp emp = new tble_Emp();
OperationDataDataContext dc = new OperationDataDataContext();
int q = Convert.ToInt32(id);
var employess = from emps in dc.tble_Emps
join depts in dc.tble_depts
on emps.deptid equals depts.id
where q == emps.EmpId
select emps;
// if (company == null)
// return "Company cannot be found";
dc.tble_Emps.DeleteAllOnSubmit(employess);
dc.SubmitChanges();
return "ok";
}
catch (Exception ex)
{
return ex.Message;
}
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments