Below you will find the specific code that I am attempting to optimize. I have a method previous to called getCount()
which, based on similar parameters as below, calculates how many record sets of 75 will be pulled based on one of the below query.
The Query that is executed is based on the department
, projLead
and status
string while projSet
is used to skip records.
So basically, count returns the number of iterations that will need to be performed to obtain the full result set. An ajax call inside a for
loop iterates until i
> the last projSet()
and calls the below method.
The problem with this is that every time it gets the next sub result set of 75 records it needs to go through the if
structure below. Is there a way to basically have the below method determine which query to use, return the query as a String
or something and then in another method pass the queryString
and then run it as a query until the number of sets is greater than the total number of sets requiring pulling? This way, the if structure is just used to determine the query which can be passed into another method with just the query being pass and run inside it and it returns the result sets.
The iterative part would be handled via ajax, but I do not know how to return the query and have it passed into another method through another ajax call where it just executes the query.
public ActionResult ProjList(String department, String projLead, String status, int projSet)
{
//Query where all filters are applied
var query = db.LatestStatus.Where(x => (x.Department.CompareTo(department) == 0)
&& (x.ProjectLeader.CompareTo(projLead) == 0)
&& (x.Status.CompareTo(status) == 0))
.OrderBy(x => x.ProjectID).Skip(projSet * 75).Take(75).ToList();
//Check to see which filters are not applied
// - > all filters are not applied
// - > find which filters are applied and return it's query
if (department == null || (department.Equals("Department")
&& projLead.Equals("Project Leader")
&& status.Equals("Status")))
{
query = db.LatestStatus
.OrderBy(x => x.ProjectID)
.Skip(projSet * 75).Take(75).ToList();
}
else if (projLead.Equals("Project Leader")){
if (status.Equals("Status"))
{
query = db.LatestStatus
.Where(x => x.Department.CompareTo(department) == 0)
.OrderBy(x => x.ProjectID).Skip(projSet * 75).Take(75).ToList();
}
else if (!department.Equals("Department"))
{
query = db.LatestStatus
.Where(x => (x.Department.CompareTo(department) == 0) && (x.Status.CompareTo(status) == 0))
.OrderBy(x => x.ProjectID).Skip(projSet * 75).Take(75).ToList();
}
else
{
query = db.LatestStatus
.Where(x => x.Status.CompareTo(status) == 0)
.OrderBy(x => x.ProjectID).Skip(projSet * 75).Take(75).ToList();
}
}
else if (department.Equals("Department")){
if (status.Equals("Status"))
{
query = db.LatestStatus
.Where(x => x.ProjectLeader.CompareTo(projLead) == 0)
.OrderBy(x => x.ProjectID).Skip(projSet * 75).Take(75).ToList();
}
else if (!department.Equals("Project Leader"))
{
query = db.LatestStatus
.Where(x => (x.Status.CompareTo(status) == 0) && (x.ProjectLeader.CompareTo(projLead) == 0))
.OrderBy(x => x.ProjectID).Skip(projSet * 75).Take(75).ToList();
}
else
{
query = db.LatestStatus
.Where(x => x.Status.CompareTo(status) == 0)
.OrderBy(x => x.ProjectID).Skip(projSet * 75).Take(75).ToList();
}
}
else if (department.Equals("Status")){
if (status.Equals("Department"))
{
query = db.LatestStatus
.Where(x => x.ProjectLeader.CompareTo(projLead) == 0)
.OrderBy(x => x.ProjectID).Skip(projSet * 75).Take(75).ToList();
}
else if (!department.Equals("Project Leader"))
{
query = db.LatestStatus
.Where(x => (x.Department.CompareTo(department) == 0) && (x.ProjectLeader.CompareTo(projLead) == 0))
.OrderBy(x => x.ProjectID).Skip(projSet * 75).Take(75).ToList();
}
else
{
query = db.LatestStatus
.Where(x => x.ProjectLeader.CompareTo(projLead) == 0)
.OrderBy(x => x.ProjectID).Skip(projSet * 75).Take(75).ToList();
}
}
return View(query);
}
getCount()
andcount
? I don't seee this in your code. – Heslacher 2 days ago