LINQ的LEFT JOIN产生NULL引用异常

时间:2016-09-01 19:02:09

标签: c# linq join left-join

我正在尝试使用LINQ将当前代码使用JOIN转换为LEFT JOIN。这是当前的代码



globalFunctions = _rolesAdapter.GetSecurityRightsForRole(SysManagerConstants.GLOBAL_DEFAULTS_ROLE_NUMBER, "SecFunc").
                Select(secRight => new FuncViewModel()
                {
                    Descrip = secRight.SecFunc.Descrip,
                    FuncCode = secRight.SecFunc.FuncCode,
                    Grouping = secRight.SecFunc.Grouping,
                    Progmodule = secRight.SecFunc.Progmodule,
                    SubCat = secRight.SecFunc.SubCat,
                    GrantPerm = secRight.GrantPerm
                }).OrderBy(x => x.Descrip);

            //Unassigned
            editSecRoleViewModel.UnassignedFunctions = editSecRoleViewModel.UnassignedFunctions.Join(globalFunctions, uf => uf.FuncCode, gf => gf.FuncCode,
                    (uf, gf) => new FuncViewModel()
                    {
                        Descrip = uf.Descrip,
                        FuncCode = uf.FuncCode,
                        Grouping = uf.Grouping,
                        Progmodule = uf.Progmodule,
                        SubCat = uf.SubCat,
                        GrantPerm = (byte)(gf.GrantPerm | uf.GrantPerm),
                        DefaultGrantPerm = gf.GrantPerm
                    }).OrderBy(x => x.Descrip);




我尝试转换为LEFT JOIN基于 How do you perform a left outer join using linq extension methods



//Unassigned
            editSecRoleViewModel.UnassignedFunctions = editSecRoleViewModel.UnassignedFunctions.GroupJoin(globalFunctions, 
                uf => uf.FuncCode, gf => gf.FuncCode,
                    (uf, gf) => new FuncViewModel()
                    {
                        Descrip = uf.Descrip,
                        FuncCode = uf.FuncCode,
                        Grouping = uf.Grouping,
                        Progmodule = uf.Progmodule,
                        SubCat = uf.SubCat,
                        GrantPerm = (byte)(((int?)gf.FirstOrDefault().GrantPerm??1) | uf.GrantPerm),
                        DefaultGrantPerm = (byte) ((int?)gf.FirstOrDefault().GrantPerm ?? 1)
                    }).OrderBy(x => x.Descrip);




我尝试了SingleOrDefault和FirstOrDefault,并且我在运行时继续获取NULL对象引用。样本和我的案例之间的区别在于我创建了一个新的FuncViewModel对象而不是新对象。应该怎么做才能解决问题?

1 个答案:

答案 0 :(得分:0)

我用这段代码解决了它

 //Unassigned
            editSecRoleViewModel.UnassignedFunctions = editSecRoleViewModel.UnassignedFunctions.SelectMany( 
                uf => globalFunctions.Where(gf => uf.FuncCode== gf.FuncCode).DefaultIfEmpty(),
                    (uf, gf) => new FuncViewModel()
                    {
                        Descrip = uf.Descrip,
                        FuncCode = uf.FuncCode,
                        Grouping = uf.Grouping,
                        Progmodule = uf.Progmodule,
                        SubCat = uf.SubCat,
                        GrantPerm = (byte)( ((gf==null)?1:gf.GrantPerm) | uf.GrantPerm) ,
                        DefaultGrantPerm = ((gf == null) ? (byte)1 : gf.GrantPerm)
                    }).OrderBy(x => x.Descrip);