2
\$\begingroup\$

I hate having almost two identical blocks do almost the exact same thing.

I am working with Entity Framework 4.4.0.0
Asp.Net MVC

Right 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);
}
\$\endgroup\$
0

1 Answer 1

2
\$\begingroup\$

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.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.