I have the following problem. I am new to using LINQ in C# and I have the following code:
var empleado = db.Employees.Where(x => x.EmployeeID ==numReloj).ToList();
var ID = empleado[0].ID;
var employeec = db.Employee_Course.Where(x => x.ID_EMPLEADO == ID).ToList();
var idcourse = employeec[0].ID_CURSO;
var cursos = db.Courses.Where(x => x.ID_CURSO == idcourse).ToList();
Here what the code does is take into account the employee's clock number and I need to see what courses they have, but the idcourse variable only grabs the first element. You know how I can make it go through all the data in that table.
Obviously you only get the first element, that's what you're saying in employeec[0] -> the fact of using
[]
and assigning a number between these signs, you're saying that you want a specific element from an index.Replace this:
For this:
Now it's time to see the entire list of courses that have an ID in the list we just defined:
Finally, to iterate through all the elements of the courses, a classic foreach: