Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 过滤实体框架6中存在联接表的相关实体或导航属性_C#_Asp.net_Entity Framework_Entity_Entity Framework 6 - Fatal编程技术网

C# 过滤实体框架6中存在联接表的相关实体或导航属性

C# 过滤实体框架6中存在联接表的相关实体或导航属性,c#,asp.net,entity-framework,entity,entity-framework-6,C#,Asp.net,Entity Framework,Entity,Entity Framework 6,我有三个实体Role,RoleUser和User var result = DataContext.Role.Where(role => role.Compulsory == true ) .Include(gl => gl.RoleUser) .Select(x => new { role = x, r

我有三个实体
Role
RoleUser
User

var result = DataContext.Role.Where(role => role.Compulsory == true )
                .Include(gl => gl.RoleUser)
                .Select(x => new
                {
                    role = x,
                    roleUsers= x.RoleUser.Where(g => g.Id == 1),
                }).ToList();
我想选择每个必选的
角色
,并加载相关的
用户
,其中join表中的
RoleUser.RecordID
等于给定值

使用UOW和GenericReposicity
Get(…)
方法,我将调用

.Get(role=>role.privative==true,null,“RoleUser.User”)
选择所有必需的角色,并为导航加载所有
RoleUser.User

如何过滤它们,是否可以使用实现的
Get()
方法

实体

    public class Role
    {
        public int RoleID { get; set; }

        public bool Compulsory { get; set; }

        public virtual IList<RoleUser> RoleUser { get; set; }       
    }


    public class RoleUser
    {
        public string UserID { get; set; }

        public virtual User User { get; set; }


        public Guid RecordID { get; set; }

        public virtual Record Record { get; set; }


        public int RoleID { get; set; }

        public virtual Role Role { get; set; }
    }


    public class User
    {
        public string userID { get; set; }
    }
公共类角色
{
public int RoleID{get;set;}
公共布尔强制{get;set;}
公共虚拟IList RoleUser{get;set;}
}
公共类角色扮演者
{
公共字符串用户标识{get;set;}
公共虚拟用户用户{get;set;}
公共Guid记录ID{get;set;}
公共虚拟记录记录{get;set;}
public int RoleID{get;set;}
公共虚拟角色{get;set;}
}
公共类用户
{
公共字符串用户标识{get;set;}
}
获取

    public virtual IList<TEntity> Get(
        Expression<Func<TEntity, bool>> filter = null,
        Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
        string includeProperties = "")
    {
        IQueryable<TEntity> query = dbSet;

        if (filter != null)
        {
            query = query.Where(filter);
        }

        foreach (var includeProperty in includeProperties.Split
            (new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
        {
            query = query.Include(includeProperty);
        }

        if (orderBy != null)
        {
            return orderBy(query).ToList();
        }
        else
        {
            return query.ToList();
        }
    }
公共虚拟IList获取(
表达式筛选器=空,
Func orderBy=null,
字符串includeProperties=“”)
{
IQueryable query=dbSet;
if(过滤器!=null)
{
query=query.Where(过滤器);
}
foreach(includeProperty.Split中的var includeProperty
(新字符[]{',},StringSplitOptions.RemoveEmptyEntries)
{
query=query.Include(includeProperty);
}
if(orderBy!=null)
{
returnorderby(query.ToList();
}
其他的
{
返回query.ToList();
}
}
不,您不能从该Get方法中筛选
角色扮演者
。如果要筛选
RoleUser
,则需要将它们投影到另一个列表中

您可以根据
RoleUser
表筛选您的
角色

Include将始终在表中显示完整数据,除非您使用iquirable投影

这将仅基于
角色扮演者
过滤器过滤
角色

如果需要筛选
Include
表,请编写查询,以便将
IQuerable
发送到数据库

尝试类似的操作,这将过滤
角色
角色

var result = DataContext.Role.Where(role => role.Compulsory == true )
                .Include(gl => gl.RoleUser)
                .Select(x => new
                {
                    role = x,
                    roleUsers= x.RoleUser.Where(g => g.Id == 1),
                }).ToList();