locked
Filter choice list in a new data screen based on current user department. RRS feed

  • Question

  • Hello,

    I am creating a simple task manager with employees and departments. There are 3 tables: 

    Users: Contains the name, email and department

    Departments: Contains the name of the department.

    Tasks: Contains dates and descriptions related to the task.

    There are relationships between users:departments and users:tasks

    I have set permissions to differentiate between a normal user and a department manager, the department manager is able to create new tasks for the department and the normal user can't.

    The problem is, I can't filter the users that appear as a possible choice when the department manager creates a new task. A dropdown menu appears and displays all the employees, when only the employees from the department of the currently logged manager should appear.

    I tried doing this with a filter, with the code below.

     
            partial void UsersSet_Filter(ref Expression<Func<Users, bool>> filter)
            { 
                bool admin = this.Application.User.HasPermission(Permissions.Admin);
                bool ceoAssistant = this.Application.User.HasPermission(Permissions.CEOAssistant);
                bool manager = this.Application.User.HasPermission(Permissions.DepartmentManager);
                bool normal = this.Application.User.HasPermission(Permissions.NormalUser);
                string user = this.Application.User.Name.Trim();
                string department = (from r in UsersSet where r.Name.Trim() == user select r.Departments.Name).ToString();
    
                if (normal == true)
                {
                    filter = (p => p.Name.Trim() == user);
                }
                if (manager == true)
                {
                    filter = (p => p.Departments.Name == department);
                }
    
    
    
            }

    The department string does not get the value of the department, instead, it is set to this: "Microsoft.LightSwitch.ServerGenerated.Implementation.QueryImplementation`1[System.String]"

    Does anyone have any idea of how can I get the desired results?

    Thanks in advance

    Pablo.

    Tuesday, December 22, 2015 3:23 PM

Answers

  • Weiwei's code won't work because it does not return a single user (either by FirstOrDefault or SingleOrDefault) before trying to access a singular user's department.  To make that code work, try this:

    Department department = this.DataWorkspace.ApplicationData.UserSet.where(a.Department != null && a.Name.Trim() == user).FirstOrDefault().Department;
    ...
    if (manager == true)
    {
       filter = (p => p.Departments.Name == department.Name);
    }

    • Edited by Hessc Wednesday, December 23, 2015 7:17 PM
    • Proposed as answer by Angie Xu Thursday, January 7, 2016 2:03 AM
    • Marked as answer by Angie Xu Thursday, January 7, 2016 2:03 AM
    Wednesday, December 23, 2015 7:14 PM

All replies

  • Hi Asfalter,

    According to your code, the code of get department based on user's department name, return a department object, which is a Department class, not a string.

    Please convert the searching result to Department and then get the Name property of Department.

    Department department = (from r in UsersSet where r.Name.Trim()== user select r.Departments.Name).ToString();

    ...

    if (manager == true)
    {
       filter
    = (p => p.Departments.Name == department.Name);
    }

    Best Regards,
    Weiwei

    Wednesday, December 23, 2015 9:55 AM
    Moderator
  • This will get you the string department name.  It's not clear to me why departments is plural if a user can have 0 or 1 department, but this should work.

    string department = this.DataWorkspace.UserSet.where(a => a.Departments != null && a.Name.Trim() == user).FirstOrDefault().Departments.Name;

    Wednesday, December 23, 2015 2:08 PM
  • Hello Hescc,

    Thanks for your help, I tried running the code you provided and resulted in a StackOverflowException. The FirstOrDefault() function is causing it, I'm not sure why.

    Wednesday, December 23, 2015 2:43 PM
  • Hello Weiwei,

    Thanks for your help, how can I convert the searching result to a Department type? I have tried casting to no avail. Your code results in a "Cannot implicitly convert type 'string' to 'LightswitchApplication.Departments'". 

    Wednesday, December 23, 2015 3:35 PM
  • I forgot to add the datasource in my code!!  Use ApplicationData or whatever your datasource is called.

    Try this first:

    string department = this.DataWorkspace.ApplicationData.UserSet.where(a => a.Departments != null && a.Name.Trim() == user).FirstOrDefault().Departments.Name;
    If that doesn't work, try making department singular instead of plural.  If Departments is a foreign key to Users, it should be singular even if the table name is departments plural.  Watch the options that intellisense is giving you as you type (a => a.) it and use what it suggests after the "a."  If it is Departments, use that.  If it is Department, use that.  If neither show up in intellisense, you may have a problem with how the relationship is set up.
    Wednesday, December 23, 2015 7:12 PM
  • Weiwei's code won't work because it does not return a single user (either by FirstOrDefault or SingleOrDefault) before trying to access a singular user's department.  To make that code work, try this:

    Department department = this.DataWorkspace.ApplicationData.UserSet.where(a.Department != null && a.Name.Trim() == user).FirstOrDefault().Department;
    ...
    if (manager == true)
    {
       filter = (p => p.Departments.Name == department.Name);
    }

    • Edited by Hessc Wednesday, December 23, 2015 7:17 PM
    • Proposed as answer by Angie Xu Thursday, January 7, 2016 2:03 AM
    • Marked as answer by Angie Xu Thursday, January 7, 2016 2:03 AM
    Wednesday, December 23, 2015 7:14 PM
  • Hi Asfalter,

    Thanks for Hessc's reply.

    As Hessc said, the code return a collection of Department. I forget to add the FirstOrDefault() method. Sorry for my careless.

    Hessc's code should work. Please try with it again.

    Best Regards,
    Weiwei

    Thursday, December 24, 2015 1:16 AM
    Moderator