LINQ Join to delete contents from multiple tables

Jinto John

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;
    }
}
Jinto John
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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

DELETE rows from multiple tables with JOIN in Mysql

From Dev

MySql: delete from multiple tables using left join

From Dev

Linq join on multiple tables with null values

From Dev

Join 2 tables with multiple references in LINQ

From Dev

JOIN vs Multiple FROM Tables

From Dev

Delete from multiple tables with MyISAM

From Dev

Delete from multiple tables, if has

From Dev

How to scrape contents from multiple tables in a webpage

From Dev

DELETE FROM 2 tables with JOIN (SQL)

From Dev

Delete data from two tables with INNER JOIN

From Dev

Delete from 2 tables using INNER JOIN

From Dev

MySQL join multiple columns from multiple tables

From Dev

Postgresql delete multiple rows from multiple tables

From Dev

UNION or JOIN for SELECT from multiple tables

From Dev

Why select from multiple tables without join

From Dev

Getting data from multiple tables using a join

From Dev

SQL SELECT from multiple tables or JOIN

From Dev

LEFT JOIN on multiple tables in "FROM" part

From Dev

Fetch records from multiple tables using join

From Dev

Why select from multiple tables without join

From Dev

Retrieve records from Multiple tables using Join

From Dev

Selecting from multiple tables with semi-join

From Dev

Using LINQ to select all from multiple tables

From Dev

Select Distinct from multiple tables linq to sql

From Dev

how to select data from multiple tables with linq

From Dev

MySQL - Delete from multiple tables using a UNION?

From Dev

Delete the same set of values from multiple tables

From Dev

Delete rows from multiple tables as one query

From Dev

Query to delete from multiple tables in Access