简体   繁体   English

过滤相关实体或导航属性,其中实体框架 6 中有连接表

[英]Filter related entities or navigation property where there is a join table in Entity Framework 6

I have three entities Role , RoleUser and User我有三个实体RoleRoleUserUser

I want to select each Compulsory Role and load the related User 's where the RoleUser.RecordID in the join table is equal to a given value.我想选择每个强制Role并加载相关的User ,其中连接表中的RoleUser.RecordID等于给定值。

Using the UOW and GenericReposiity Get(...) method I would call ...使用 UOW 和 GenericReposiity Get(...)方法我会调用...

.Get(role => role.Compulsory == true, null, "RoleUser.User") to select all the compulsory role's and load all User for the navigation RoleUser.User . .Get(role => role.Compulsory == true, null, "RoleUser.User")选择所有强制角色并加载导航RoleUser.User所有User

How can I filter them, is it possible using the implemented Get() method?如何过滤它们,是否可以使用实现的Get()方法?

Entities实体

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

Get得到

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

No , you cant filter the RoleUser from this Get method.不,您不能从此 Get 方法过滤RoleUser if you want to filter RoleUser you want to project them into another list.如果要过滤RoleUser将它们投影到另一个列表中。

You can filter your Role based on the RoleUser table.您可以根据RoleUser表过滤您的Role

Include will always bring full data in the table unless you use IQuerable Projection.除非您使用 IQerable Projection,Include 将始终在表中显示完整数据。

var rolesWithAnyUserName = UnitOfWork.Role
                                     .Get(role => role.Compulsory == true && role.RoleUser
                                   .Any(g=>g.RoleUserName=="Name"), null, "RoleUser.User") 

This will only filter the Role based on the RoleUser filter这只会根据RoleUser过滤器过滤Role

If you need to filter Include tables write the query where you can send IQuerable to the database如果您需要过滤Include表,请编写查询,您可以将IQuerable发送到数据库

Try something like this , this will filter both Role and RoleUser尝试这样的事情,这将过滤RoleRoleUser

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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 与实体框架对象结合使用查询。 导航属性仍显​​示联接实体的所有实体 - using Join with entity framework object query. Navigation property still shows all the entities of the joined entity 实体框架性能连接与导航属性 - Entity framework performance join vs navigation property 筛选器导航属性实体框架,返回NULL - Filter Navigation Property Entity Framework, returns NULL 避免在带有Entity Framework导航属性的Sql Server表中使用自引用实体重复 - Avoid duplicates with self-referencing entities in Sql Server table with Entity Framework navigation property 实体框架,带有where条件的相关实体的常规加载 - Entity Framework, Generic loading of related entities with a where condition 实体框架6.1.3导航属性仅在查询相关表后才有值 - Entity Framework 6.1.3 navigation property only has value after query to related table 如果是通用实体,如何在Entity Framework 6中设置导航属性 - How to set navigation property in Entity Framework 6 in case of generic entities Entity Framework 相关实体生成同一张表的不必要的连接 - Entity Framework related entities generate unnecessary joins of the same table 首先使用Entity Framework代码将2个相关实体合并到1个表中 - 2 related entities into 1 table using Entity Framework code first 实体框架加载相关实体 - Entity Framework Loading Related Entities
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM