I am trying to convert current code using JOIN to the LEFT JOIN using LINQ. This is the current code
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);
My attempt to convert into LEFT JOIN based on the 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);
I tried SingleOrDefault and FirstOrDefault and I keep getting the NULL object reference in run-time. The difference between the sample and my case is that I'm creating a new FuncViewModel object instead of just new. What should be done to correct the problem?