James Wilson
James Wilson

Reputation: 5150

LINQ query structure with if statement?

I have a full LINQ query built out. I need one query run if the user is a manager/team lead and a separate query run if they are not. However the queries are very similar.

Manager/Teamleads get to see everything. Where as others only see what they are certified for.

var model = (from d in 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
                // Non-Manager/Teamlead portion
                join cert in db.IPACS_Certification on sop.procedureID equals cert.procedureID
                join procdoc in db.IPACS_ProcedureDocs on sop.procedureID equals procdoc.procedureID
                join doc in db.IPACS_Document on procdoc.documentID equals doc.documentID
                where cert.adUserName == currUser && cert.certifiedDate > doc.dateApproved
                // End non-manager/team lead
                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,
                    automated = (bool)sop.automated
                });

Is what I am trying to do above possible without creating two separate var model = statements?

Upvotes: 0

Views: 1277

Answers (1)

James S
James S

Reputation: 3588

why not just create a query, and then if not a teamleader add the additional join/where clauses before the query is sent to the data source

ie:

var query = (from d in 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 {d, f, pg, sop});



if (!TeamLeader){
        query = from x in query
            // Non-Manager/Teamlead portion
            join cert in db.IPACS_Certification on x.sop.procedureID equals cert.procedureID
            join procdoc in db.IPACS_ProcedureDocs on x.sop.procedureID equals procdoc.procedureID
            join doc in db.IPACS_Document on procdoc.documentID equals doc.documentID
            where cert.adUserName == currUser && cert.certifiedDate > doc.dateApproved
            // End non-manager/team lead
            select x;
}

var model = (from x in query
            select new IPACS_DT_MasterList
            {
                departmentID = x.d.departmentID,
                functionID = x.f.functionID,
                processID = x.pg.processID,
                procedureID = x.sop.procedureID,
                departmentName = x.d.name,
                functionName = x.f.name,
                processName = x.pg.name,
                procedureName = x.sop.name,
                owner = x.sop.owner,
                automated = (bool)x.sop.automated
            });

Upvotes: 2

Related Questions