gpt4 book ai didi

c# - Entity Framework - n 层 - 多对多 - 如何获取列表 "where"

转载 作者:太空宇宙 更新时间:2023-11-03 12:38:10 27 4
gpt4 key购买 nike

多对多类

enter image description here

在数据库中我的表是

enter image description here

enter image description here

enter image description here

我使用 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

这不是真的,我想在群里找类(class)

Find Lessons where Group Id is

最佳答案

您必须检查与该类(class)相关的组是否包含指定的组 id

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

但为什么不获取与指定组相关的类(class)呢?

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

return grp.lesson.ToList();
}

关于c# - Entity Framework - n 层 - 多对多 - 如何获取列表 "where",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40128487/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com