日期:2014-05-20  浏览次数:20948 次

linq to entity 子查询 高手帮助
C# code
 public static List<KL_Prem_SysFun> GetMenu(int roleid)
        {
            var queryRoleRight = from KL_Prem_RoleRight in Entities.KL_Prem_RoleRight.Where(p => p.RoleId == roleid) select KL_Prem_RoleRight;

            var querySysFun = from KL_Prem_SysFun in Entities.KL_Prem_SysFun.Where(p => p.ParentNodeId == 0) select KL_Prem_SysFun;

            List<KL_Prem_SysFun> listSysfun = new List<KL_Prem_SysFun>();
            KL_Prem_SysFun sysfunmodel;
            foreach (var sysfun in querySysFun)
            {
                foreach (var right in queryRoleRight)
                {
                    if (sysfun.NodeId==right.NodeId)
                    {
                        sysfunmodel = new KL_Prem_SysFun();
                        sysfunmodel.DisplayName = sysfun.DisplayName;
                        sysfunmodel.DisplayOrder = sysfun.DisplayOrder;
                        sysfunmodel.NodeId = sysfun.NodeId;
                        sysfunmodel.NodeURL = sysfun.NodeURL;
                        sysfunmodel.ParentNodeId = sysfun.ParentNodeId;
                        listSysfun.Add(sysfunmodel);

                    }
                }
            }


            return listSysfun;

        }


高手帮助啊,看这代码就知道效率很低,而且看着都头疼,有没有简便的方法。菜鸟求助
分数不多,请多关照

------解决方案--------------------
C# code

        public static List<KL_Prem_SysFun> GetMenu(int roleid)
        {
            var queryRoleRight = Entities.KL_Prem_RoleRight.Where(p => p.RoleId == roleid);
            var querySysFun = from KL_Prem_SysFun in Entities.KL_Prem_SysFun
                              where KL_Prem_SysFun.ParentNodeId == 0&& queryRoleRight.Any(a=>a.NodeId==KL_Prem_SysFun.NodeId)
                              select KL_Prem_SysFun;
            return querySysFun.ToList();
        }
       //关系
        public static List<KL_Prem_SysFun> GetMenu(int roleid)
        {
            var querySysFun = Entities.KL_Prem_SysFun.Where(w => w.ParentNodeId == 0 && w.KL_Prem_RoleRight.Any(a => a.RoleId == roleid && a.NodeId == w.NodeId));
            return querySysFun.ToList();
        }