locked
How can I filter the department for dropdown list of department according to the selected int array RRS feed

  • Question

  • User-1355965324 posted

    Some users will have different Department  Access right in different depot

    For example User A will have the Department right in Depo 1  is   Admin  and at the same time he has the right  in Depot 2   is  Marketting  and Sales.  So I am trying to make  a SelectedList for dropdown

    If the user A  and Depot 1 , then Department List  should be contain only Admin 

    If the user A  and Depot 2  then Department List  should be contain Marketting and   Sales

    If the User A and selected Depot 1  and Depot 2  then The Department List should be contain Admin, Marketting and Sales. Please help to get a linq   for selectlist Department according to the Depot and user

    My Model class

    User Model
    UserID  int 
    
    goDepartment model
    DepartmentID int 
    DepartmentName string
    
    goUserDepotDepartment model
    
    DepartmentID int 
    DepartmentName string
    UserID int
    DepotNo int
    

     Record

    Record in goDepartment 
    DepartmentID =1
    DepartmentName ='Admin'
    
    DepartmentID =2
    DepartmentName ='Marketting'
    
    DepartmentID =3
    DepartmentName ='Sales'
    
    
    goUserDepotDepartment Record
    UserID =1
    DepotNo = 1
    DepartmentID =1

    UserID =1 DepotNo = 2 DepartmentID =2

    UserID =1
    DepotNo = 2
    DepartmentID =3

    This is my code

    public IEnumerable<SelectListItem> GetLocationsByDepotForDropdown(int userId, int[] depotNo) 
    { List<SelectListItem> locations = (from dpt in goContext.goDepartment join dep in goContext.goUserDepartment on dpt.DepartmentID equals dep.DepartmentID where dep.UserID == userId &&dep.DepotNo in(depotNoArray) // Here the depot no should be only from among the arraylist) && dpt.IsDeleted == false select new SelectListItem { Value = dpt.DepartmentID.ToString(), Text = dpt.DepartmentName }).ToList<SelectListItem>(); locations.Insert(0, new SelectListItem { Value = "0", Text = "-- Select --" }); return locations; }
    }

    Friday, July 31, 2020 3:02 PM

Answers

  • User711641945 posted

    Hi polachan,

    Here is a working demo like below:

    public IEnumerable<SelectListItem> GetLocationsByDepotForDropdown(int userId, int[] depotNo)
    {
        List<SelectListItem> locations = (from dpt in goContext.goDepartment
                                            join dep in goContext.goUserDepartment
                                            on dpt.DepartmentID equals dep.DepartmentID
                                            where dep.UserID == userId
                                            &&  depotNo.Contains(dep.DepotNo)// Here the depot no should be only from among the arraylist)
    && dpt.IsDeleted == false select new SelectListItem { Value = dpt.DepartmentID.ToString(), Text = dpt.DepartmentName }).ToList<SelectListItem>(); locations.Insert(0, new SelectListItem { Value = "0", Text = "-- Select --" }); return locations; }

    My Testing Model:

    public class User
    {
        [Key]
        public int UserID { get; set; }
        public string UserName { get; set; }
    }
    public class goDepartment
    {
        [Key]
        public int DepartmentID { get; set; }
        public string DepartmentName { get; set; }
        public bool IsDeleted { get; set; }
    }
    public class goUserDepartment
    {
        public int DepartmentID { get; set; }
        public string DepartmentName { get; set; }
        public int UserID { get; set; }
        public int DepotNo { get; set; }
    
    }

    My Testing DbContext:

    public class YourDbContext: DbContext
    {
        public YourDbContext(DbContextOptions<YourDbContext> options)
            : base(options)
        {
        }
    
        public DbSet<User> User { get; set; }
        public DbSet<goDepartment> goDepartment { get; set; }
        public DbSet<goUserDepartment> goUserDepartment { get; set; }
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<goUserDepartment>().HasNoKey();
        }
    }

    Result:

    Best Regards,

    Rena

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 3, 2020 8:31 AM