score:2
To resolve changing the department of the employee: select d
should be select e
to get the employee and update the employee's Department ID, not the Department record's ID.
my department table contains department_name so if I want to update my department_name then I will be using select d am I correct?
If you want to actually change the name of that department, then you can select the department entity and change its name. However, that depends on whether that's really what you want to do. If an employee is pointing at a Department ID = 1, name = "Department A", do you want to change that Department's name to "Department B" or does a Department B already exist with a different ID? (I.e. 2) If you point the Employee's DepartmentID to "2", then the associated department details would be coming from Department B, this is typically what you'd want to occur. If you wanted to change the department's name (and the name that appears for all employees associated to Department ID 1) then you can select that department and update it's name.
Looking at your original code:
var result = (from e in DSE.employees
join d in DSE.departments on e.department_id equals d.department_id
join ws in DSE.workingshifts on e.shift_id equals ws.shift_id
where d.department_id == 1
select d).FirstOrDefault();
if (result != null)
{
result.department_id = 2;
DSE.SaveChanges();
}
The Joins are essentially unnecessary since you aren't doing anything with the department or shifts. This could be simplified to:
var employee = DSE.employees.Where(e => e.department_id == 1)
.FirstOrDefault();
if (employee != null)
{
employee.department_id = 2;
DSE.SaveChanges();
}
When using methods like FirstOrDefault
you should always include an Order By type clause to ensure you get a predictable order to get repeatable results.
If you actually want to update related data, such as the department's name:
var department = DSE.departments.Single(d => d.department_id == 1);
department.name = "New Name";
DSE.SaveChanges();
Here because we only expect one, and only 1 department to have an ID of 1, we should use Single
rather than FirstOrDefault
. If no department is found, or more than 1 department is found it will throw an exception. Better this exception telling us zero or more rows were found than returning a "OrDefault" method and tripping over a NullReferenceException
down the road.
My examples use the Fluent methods offered by EF rather than the linq QL syntax, but the same behaviour can be implemented that way. I just find the fluent methods easier to structure and chain together.
With EF, the real power comes from mapping relationships via navigation properties so you don't need to expose FK properties or manually map out Join expressions like you would in SQL. EF can manage all of this behind the scenes. You can load entities and either eager or lazy load their related entities, such as looking to update data, or simply select fields from the entity and its related details and let EF build the suitable SQL.
Source: stackoverflow.com
Related Articles
- C# WebAPI Cross update inner join data with multiple tables
- Join data from multiple tables into SelectList, and precede each dataset with a caption in MVC
- Get data from multiple tables to an object with LINQ join
- lambda expression join multiple tables with select and where clause
- How to inner join tables from different Data Context?
- How to left join multiple tables with LINQ
- LINQ to SQL: Complicated query with aggregate data for a report from multiple tables for an ordering system
- LinQ query with multiple tables and extracting data
- LINQ Join On Multiple Columns With Different Data Types
- Select data from multiple unrelated tables with LINQ to Entity Framework
- Problem with simple join by multiple columns in two tables using linq
- Entity Framework: How to perform left join with EF and LINQ among multiple tables
- How can I join datasets from multiple tables with only certain columns in EF Core?
- asp.net LinqDataSource query multiple tables with join
- Linq left join with multiple tables
- How to write SQL SELECT INNER JOIN with multiple conditions (with LIKE) query to LINQ to SQL
- join two tables with multiple condition in linq
- LINQ Group by on multiple tables with nested group by with join and aggregate
- Select multiple items across multiple tables with join
- Complex joins in Linq with multiple tables and LEFT OUTER JOIN
- How to join multiple tables to one table with lambda expression?
- Join two tables with one to many relationship and pick latest from multiple side
- How to update two columns with same name from two tables in a join query
- SQL data context with inner join
- How to split data table into multiple tables with adding 1 minute delay
- SQL to LINQ conversion with inner join and where filters on different tables
- Outer join multiple tables with flattened results
- Inner Join with multiple list
- INNER JOIN 3 Data Tables using LINQ
- Linq join on multiple tables with null values
- Linq comparing 2 dates
- Using LINQ to select item in List<T> and add integer to populate new List<T>
- LINQ Error Invalid Column when retrieving additional column value
- LINQ join query returning null
- .NET when grouping records by hour impossible to use datetime in the select
- LINQ query with array element in Where clause
- Like condition in LINQ
- Gridview sorting in ASP.NET
- How to pass predicate to linq expression
- How do I find the number of questions with answers in a survey in my database using LINQ
- Entity Framework: select property as Object
- moq.As<>().Setup() does not seem to work after getting moq.Object
- Expression tree to initialize new anonymous object with arbitrary number of properties
- LINQ. Return Matching Records from second table.
- Convert a string[] to its associated array of enums
- Asp.Net MVC3 - Linq not working in Razor Views
- Entity Framework Include in Where Subquery
- LINQ to SQL - Expression checking for null could not be translated
- Group a list of url strings based on folder structure
- Linq Expression to generic - how