entity framework - n 层 - 多对多 - 如何获取列表 "where"

entity framework - n-tier - many to many - how to get list "where"

多对多 class

在数据库中我的 table 是

我使用 EF6 和 N-Tier 并从此 post

实现通用数据访问层

我想在组里上课,我用这个方法在DAL中获取列表

    public virtual IList<T> GetList(Func<T, bool> where, params Expression<Func<T, object>>[] navigationProperties)
    {
        List<T> list;
        using (var context = new AzmaEntities())
        {
            IQueryable<T> dbQuery = context.Set<T>();
            foreach (Expression<Func<T, object>> navigationProperty in navigationProperties)
                dbQuery = dbQuery.Include<T, object>(navigationProperty);
            list = dbQuery
                .AsNoTracking()
                .Where(where)
                .ToList<T>();
        }
        return list;
    }

在业务层:

    public IList<Lesson> GetLessonWhereGrpId(int grpId)
    {
        Group grp = new Group();
        GroupBLL grpbll = new GroupBLL();
        grp = grpbll.GetGroupById(grpId);

        return _LessonRepository.GetList(
            d => d.Group.Equals(grp)
            );
    }

调试时,我的代码生成此 SQL 查询:

DECLARE @EntityKeyValue1 AS SQL_VARIANT;
SET @EntityKeyValue1 = Null;

SELECT 
    [Extent2].[Id] AS [Id], 
    [Extent2].[Ex_Id] AS [Ex_Id], 
    [Extent2].[Name] AS [Name], 
    [Extent2].[Factor] AS [Factor]
    FROM  [dbo].[LsnToGrp] AS [Extent1]
    INNER JOIN [dbo].[Group] AS [Extent2] ON [Extent1].[Grp_Id] = [Extent2].[Id]
    WHERE [Extent1].[Lsn_Id] = @EntityKeyValue1

这不是真的,我想在群里找课程

Find Lessons where Group Id is

您必须检查与该课程相关的群组是否包含指定的群组 ID

public IList<Lesson> GetLessonWhereGrpId(int grpId)
{
    return _LessonRepository.GetList(
        d => d.Group.Any(x => x.Id == grpId)
        );
}

但为什么不获取与指定组相关的课程?

public IList<Lesson> GetLessonWhereGrpId(int grpId) 
{ 
    GroupBLL grpbll = new GroupBLL(); 
    grp = grpbll.GetGroupById(grpId, g => g.lesson); 

    return grp.lesson.ToList(); 
}