Patrick Foy
Patrick Foy

Reputation: 272

how to handle conditional rules for where statements in LINQ

I have a page with dropboxes and the dropboxes drive what set of tasks are displayed to the user. I was trying to figure out how to make where statements conditional based upon those in linq, because dissimilar to something like SQLCommand you can't just build the SQL in a string then pass the string. I tried the following, but the where filtering didn't actually happen, the final result was a full list of all possible tasks:

        public ActionResult UsersTasks(string UserID, int tasksType,int assignmentType)
    {
        IEnumerable<UAC_Users> InitialPull = new UAC_Users[0];
        IEnumerable<APT_ProjectTasks> apt_projecttasks = new APT_ProjectTasks[0];

        InitialPull = from a in db.UAC_Users
                      where a.User_ID == UserID.ToUpper() 
                      select a;

        UserID = InitialPull.First().User_ID;
        string MgrID = InitialPull.First().User_ID_Mgr;

        apt_projecttasks = from a in db.APT_ProjectTasks
                           select a;



        switch (assignmentType)
        {
            case 0:
                apt_projecttasks.Where(a => a.AssignedUser_ID == UserID);
                break;
            case 1:
                apt_projecttasks.Where(a => a.UAC_Users_AssignedUser.User_ID_Mgr == MgrID);
                break;
            case 2:
                apt_projecttasks.Where(a => a.UAC_Users_AssignedUser.User_ID_Mgr == UserID);
                break;
            case 3:
                apt_projecttasks.Where(a => a.UAC_Users.User_ID == MgrID);
                break;
        }

        switch (tasksType)
        {
            case 0:
                apt_projecttasks.Where(a => (a.ActualStartDate != null || a.FirstTask == true) && a.ActualFinishDate == null);
                break;
            case 1:
                apt_projecttasks.Where(a => a.ActualStartDate == null);
                break;
            case 2:
                apt_projecttasks.Where(a => a.ActualFinishDate != null);
                break;
        }

        long test = apt_projecttasks.Count();
        ViewBag.count = test;

        return View(apt_projecttasks);

    }

I assume this is because I'm misunderstanding the syntax of the ".where" in the enumeration, but I'm unsure of what my best course of action is now. What is the best way to handle a conditional where statement in Linq?

Upvotes: 0

Views: 70

Answers (2)

Mark Jose
Mark Jose

Reputation: 11

As CDove said above you're not assigning your values.

If you're using fluent syntax for where, I'd question the conditions being outside of the where itself.

A better approach (opinionated more readable) would be to call where, with either an inline lambda which returns the case conditonals or true for the default case. Or even abstract this business logic in to a function.

i.e. Always call the LINQ where method

Upvotes: 1

CDove
CDove

Reputation: 1950

The best way to handle it is to actually assign your values.

    apt_projecttasks = apt_projecttasks.Where(a => 
                     (a.ActualStartDate != null || a.FirstTask == true)
                     && a.ActualFinishDate == null);

As it is, your cases are happening, but they're not assigning the result to anything. Linq doesn't mutate the object against which it's called.

Upvotes: 3

Related Questions