EF6无法将Generic.List转换为Linq.IQueryable

时间:2017-10-02 18:28:04

标签: c# linq generics entity-framework-6

我正在编写一个条件查询来从三个不同的表中返回特定数据。这些表没有直接关系,所以我不得不使用连接。我还需要对数据进行分组。这就是我想出的:

    public static List<SimpleConfCommSchedule> GetSimpleConfCommSchedules(DateTime confCommSchedDate,
        short sessionId, string body = null)
    {
        using (var context = new ConferenceContext())
        {
            var confCommSched = context.ConferenceMemberSchedules
                .Join(context.ConferenceMembers,
                    s => s.AuditId,
                    m => m.AuditId,
                    (s, m) => new {s, m})
                .Join(context.Rooms,
                    sm => sm.s.RoomNumber.Value,
                    r => r.RoomId,
                    (sm, r) => new { sm, r });

            confCommSched = confCommSched.Where(smr => smr.sm.s.SessionId == sessionId && smr.sm.s.Deleted == false);

            if (!string.IsNullOrWhiteSpace(body))
                confCommSched = confCommSched.Where(smr => smr.r.Body == body);

            confCommSched = confCommSched.GroupBy(smr => new { smr.sm.s.BillNumber, smr.sm.s.MeetingDateTime, smr.sm.s.AuditId, smr.r.Location })
                .Select(smr => new
                {
                    billNumber = smr.Key.BillNumber,
                    room = smr.Key.Location,
                    datetime = smr.Key.MeetingDateTime,
                    members = smr.Select(t => new
                    {
                        displayOrder = t.sm.m.DisplayOrder,
                        memberId = t.sm.m.MemberId
                    }).ToList()
                }).ToList()
                .Select(obj => new SimpleConfCommSchedule
                {
                    BillNumber = obj.billNumber,
                    Location = obj.room,
                    MeetingStart = obj.datetime,
                    MemberIds = obj.members.Select(m => new SimpleMemberId
                    {
                        MemberId = m.memberId,
                        DisplayOrder = m.displayOrder
                    }).ToList()
                }).ToList();

            return confCommSched;
        }
    }

当我到达GroupBy时,我看到一条错误,指出我无法从源类型System.Collections.Generic.List转换为System.Linq.IQueryable。

我需要在代码中进行哪些更改才能使其生效?

1 个答案:

答案 0 :(得分:1)

错误不言自明。您的变量confCommSched与您尝试使用最后一个分配(您使用GroupBy 的地方)分配的结果类型不同。修复很简单,直接返回查询结果或使用新变量。

var result = confCommSched.GroupBy(/* everything else unchanged in your query and lambda calls etc */ );
return result;