在子查询中连接具有嵌套类型的多个表

时间:2017-09-06 15:25:53

标签: c# entity-framework

我需要从多个表创建一个“User”对象,我想在一个EF查询中执行此操作。

这是我当前的查询,但我不知道如何填写“角色”,“群组”和& “权限”属性,顺便说一下。

cachedUsers = (from u in dbContext.Application_Users
               join ug in dbContext.Application_UserGroups on u.UserID equals ug.UserID
               join g in dbContext.Application_Groups on ug.GroupID equals g.GroupID
               join gr in dbContext.Application_GroupRoles on g.GroupID equals gr.GroupID
               join r in dbContext.Application_Roles on gr.RoleID equals r.RoleID
               join rp in dbContext.Application_RolePermissions on r.RoleID equals rp.RoleID
               join p in dbContext.Application_Permissions on rp.PermissionID equals p.PermissionID
               select new SecurityUser()
               {
                   ID = u.UserID,
                   UserName = u.UserName,
                   Roles = /*how to fill the list ? */(new Role
                            {
                                RoleID = r.RoleID,
                                RoleLevel = r.RoleLevel,
                                RoleName = r.RoleName,
                                Description = r.Description,
                                Trigram = r.Trigram,
                                Groups = /*how to fill the list ? */(new SecurityGroup
                                          {
                                              GroupID = g.GroupID,
                                              GroupName = g.GroupName,
                                              RoleName = r.RoleName,
                                          }).ToList(),
                                Permissions = /*how to fill the list ? */(new Permission
                                               {
                                                   PermissionID = p.PermissionID,
                                                   ConstantName = p.ConstantName,
                                                   Title = p.Title
                                               })
                                               .ToList()
                            }).ToList()
               }
              ).ToList();

你能帮我这么做吗?

非常感谢! :)

1 个答案:

答案 0 :(得分:2)

您可以通过以下方式使用嵌套对象创建对象:

var userList =
                from r in recs
                join c in Roles on r.UserID equals c.UserID into celft
                from cnew in celft.DefaultIfEmpty()
                join g in groups on cnew.GroupID equals g.GroupID into gleft
                select new
                {
                    UserID = r.UserID,
                    Name = r.Name,
                    Email = r.Email,
                    Roles = (from c1 in Roles
                             where c1.RoleID == cnew.RoleID
                             select new
                             {
                                 c1.RoleID,
                                 Grupo = (from g1 in groups
                                          where g1.GroupID == c1.GroupID
                                          select new
                                          {
                                              g1.GroupID,
                                              g1.Name
                                          }).ToList(),
                             }).ToList(),
                };

这是完整的代码,因此您可以在控制台应用中进行测试

class Program
    {
        static void Main(string[] args)
        {
            var recs = new List<Users> {
    new Users { Name = "Alex", Email = "A", UserID= 1 },
    new Users { Name = "Juan", Email = "B", UserID= 2 },
    new Users { Name = "Peter", Email = "C", UserID= 3 },
    new Users { Name = "Julios", Email = "D", UserID= 4 },
    new Users { Name = "Dennis", Email = "E", UserID= 5 },
    new Users { Name = "Jhon", Email = "F", UserID= 6 },
};
            var groups = new List<Group> {
    new Group { GroupID= 1, Name = "N1" },
    new Group { GroupID= 2, Name = "N2" },
};
            var Roles = new List<Roles> {
    new Roles { UserID= 1, RoleID = 1 , GroupID = 1 },
    new Roles { UserID= 1, RoleID = 2 , GroupID = 1},
    new Roles { UserID= 2, RoleID = 3 , GroupID = 2},
};


            var userList =
                from r in recs
                join c in Roles on r.UserID equals c.UserID into celft
                from cnew in celft.DefaultIfEmpty()
                join g in groups on cnew.GroupID equals g.GroupID into gleft
                select new
                {
                    UserID = r.UserID,
                    Name = r.Name,
                    Email = r.Email,
                    Roles = (from c1 in Roles
                             where c1.RoleID == cnew.RoleID
                             select new
                             {
                                 c1.RoleID,
                                 Grupo = (from g1 in groups
                                          where g1.GroupID == c1.GroupID
                                          select new
                                          {
                                              g1.GroupID,
                                              g1.Name
                                          }).ToList(),
                             }).ToList(),
                };

            foreach (var item in userList)
            {
                Console.WriteLine(string.Format("{0} {1} {2} {3}", item.UserID, item.Name, item.Email));
            }
            Console.ReadLine();
        }
    }

    class Users
    {
        public int UserID;
        public string Name;
        public string Email;
    }

    class Roles
    {
        public int UserID;
        public int RoleID;
        public int GroupID;
    }

    class Group
    {
        public int GroupID;
        public string Name;
    }