Question

I have the following Linq (lambda) expression:

public IEnumerable<SetupShift> GetEmployeeShift(int employeeId)
        {
            var shifts = DbContext.Set<Employee>()
                .Include(em=> em.SetupShiftCode.SetupShifts)
                .Where(em=> em.EmployeeId == employeeId)
                .Select(em => em.SetupShiftCode.SetupShifts).ToList();

            return shifts;
        }

An Employee has a ShiftCode (description), and a ShiftCode has many SetupShifts (collection of days with Intimes, etc).

I want to return only SetupShifts but when I mouse over ToList(); the result is List<ICollection<SetupShifts>> instead of only List<SetupShifts>.

Any clue?

Was it helpful?

Solution

Use SelectMany instead of Select.

var shifts = DbContext.Set<Employee>()
            .Include(em=> em.SetupShiftCode.SetupShifts)
            .Where(em=> em.EmployeeId == employeeId)
            .SelectMany(em => em.SetupShiftCode.SetupShifts).ToList();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top