1. 程式人生 > >linq To SQl之多表聯合查詢

linq To SQl之多表聯合查詢

方法一:資料控制元件繫結   

protected void LinqServerModeDataSource1_Selecting(object sender, DevExpress.Data.Linq.LinqServerModeDataSourceSelectEventArgs e)
        {

            ASPxGridView1.KeyFieldName = "RoleID";
            e.KeyExpression = "RoleID";
            e.QueryableSource = from r1 in eDataContext.Role
                                join r2 in eDataContext.Role on r1.ParentRoleID equals r2.RoleID
                                where r1.IsDeleted == false
                                select new { ParentRoleID = r1.ParentRoleID, RoleID = r1.RoleID, RoleName = r1.RoleName, RoleDesc = r1.RoleDesc, ParentRoleName = r2.RoleName };

        }

方法二:普通繫結方法中設定聯合主鍵(使用者組別編號和使用者編號組合而成)

        Casagroup.Entities.EntitiesDataContext eDataContext = new Casagroup.Entities.EntitiesDataContext();
        Common _common = new Common();
        private void BindUserInfo()
        {
            UserGroup c = new UserGroup();

 
            //聯合查詢使用者資訊
            var result = from ugu in eDataContext.UserGroupUser
                         join us in eDataContext.User on ugu.UserID equals us.UserID
                         join ug in eDataContext.UserGroup on ugu.UserGroupID equals ug.UserGroupID
                         select new { ID = ugu.ID + "_" + us.UserID, UserPWD = us.UserPWD, UserGroupID = ugu.UserGroupID, UserID = us.UserID, UserName = us.UserName, UserGroupName = ug.UserGroupName, IsEnabled = us.IsEnabled };

            this.gvUser.DataSource = result;
            this.gvUser.DataBind();

        }