Linq to Entities Multiple Includes,但只选择一个关系

时间:2014-04-29 23:43:25

标签: linq entity-framework linq-to-entities

我有以下Linq(lambda)表达式:

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;
        }

员工拥有ShiftCode(描述),ShiftCode有许多SetupShifts(使用Intimes的天数集合等)。

我想只返回SetupShifts但是当我将鼠标移到ToList()上时;结果是List<ICollection<SetupShifts>>而不是List<SetupShifts>

有任何线索吗?

1 个答案:

答案 0 :(得分:0)

使用SelectMany代替Select

var shifts = DbContext.Set<Employee>()
            .Include(em=> em.SetupShiftCode.SetupShifts)
            .Where(em=> em.EmployeeId == employeeId)
            .SelectMany(em => em.SetupShiftCode.SetupShifts).ToList();