I hate having almost two identical blocks do almost the exact same thing.I am working with Entity Framework 4.4.0.0Asp.Net MVCRight now if the user is a manager I want to show them the entire list, if they are a normal user or a Team Lead.  I only want to show them their specific department.  The only different from both model is that one add's a where and the other does not.public ActionResult Index(){    AD_CurrentUserProfile currentUP = new ActiveDirectory().GetUserProfile(User.Identity.Name.Split(\\.ToCharArray())[1].ToString());    IQueryable<IPACS_DT_MasterList> model;    if (currentUP.currentUser.isManager)    {        model = (from d in db.IPACS_Department                        join f in db.IPACS_Function on d.departmentID equals f.departmentID                        join pg in db.IPACS_Process on f.functionID equals pg.functionID                        join sop in db.IPACS_Procedure on pg.processID equals sop.processID                        select new IPACS_DT_MasterList                        {                            departmentID = d.departmentID,                            functionID = f.functionID,                            processID = pg.processID,                            procedureID = sop.procedureID,                            departmentName = d.name,                            functionName = f.name,                            processName = pg.name,                            procedureName = sop.name,                            owner = sop.owner                        });    }    else    {        model = (from d in db.IPACS_Department                        where d.name == currentUP.currentUser.department // only change                        join f in db.IPACS_Function on d.departmentID equals f.departmentID                        join pg in db.IPACS_Process on f.functionID equals pg.functionID                        join sop in db.IPACS_Procedure on pg.processID equals sop.processID                        select new IPACS_DT_MasterList                        {                            departmentID = d.departmentID,                            functionID = f.functionID,                            processID = pg.processID,                            procedureID = sop.procedureID,                            departmentName = d.name,                            functionName = f.name,                            processName = pg.name,                            procedureName = sop.name,                            owner = sop.owner                        });    }    return View(model);}
Restricting search to a user's own department, if the user is not a manager
c#;linq;entity framework;asp.net mvc 4
Merge your if statement into the where clause.public ActionResult Index(){    AD_CurrentUserProfile currentUP = new ActiveDirectory().GetUserProfile(User.Identity.Name.Split(\\.ToCharArray())[1].ToString());    IQueryable<IPACS_DT_MasterList> model =        (from d in db.IPACS_Department                where (currentUP.currentUser.isManager || d.name == currentUP.currentUser.department)                join f in db.IPACS_Function on d.departmentID equals f.departmentID                join pg in db.IPACS_Process on f.functionID equals pg.functionID                join sop in db.IPACS_Procedure on pg.processID equals sop.processID                select new IPACS_DT_MasterList                {                    departmentID = d.departmentID,                    functionID = f.functionID,                    processID = pg.processID,                    procedureID = sop.procedureID,                    departmentName = d.name,                    functionName = f.name,                    processName = pg.name,                    procedureName = sop.name,                    owner = sop.owner                });    return View(model);}If the current user is a manager, the first condition will return true and cause the where statement to short-circuit; otherwise it will compare the department name to the user's department.