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?

有帮助吗?

解决方案

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();
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top