locked
How can make the Selected property True in the given LINQ RRS feed

  • Question

  • User-1355965324 posted

    I am trying to give Selected property true for a drop down depends  the value of the record. I have the following model#

    User  model
    public UserId int 
    public DefaultDepotNo int
    public DefaultDepartmentId int
    

    The above table contain the following Record

    UserId = 1 ,  DefaultDepot =1, DefaultDepartment = 2
    user = 2 ,     DefaultDepot = 2, DefaultDepartment = 1

    I have the following Linq to  get the drop down for the department. It is working fine . But   I want to get the Department is to be selected true  against the department listed in dropdown

    int Userid = 2
    int[] depotNo = {1,2)
    
    So I have to include one more condition in this linq
    If Int array DepotNo contain the user DefaultDepot, and  if the Values in SelectListItem 
    contain his DefaultDepartment, that department should be selected as true other department should be Selected = false. Is there any way can make it one linq 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) && dep.IsDeleted == false select new SelectListItem { Value = dpt.DepartmentID.ToString(), Text = dpt.DepartmentName, Selected = if the value exist in user table as defaultdepartment
    and int[] depotNo contain his DefaultDepot
    }).Distinct(). ToList<SelectListItem>(); locations.Insert(0, new SelectListItem { Value = "0", Text = "-- Select --" }); return locations;

    Monday, August 3, 2020 2:12 PM

Answers

  • User711641945 posted

    Hi polachan,

    Please share your whole model when you ask the questions otherwise we would waste much time building models for your issue.The different model would make different results.

    Here is a working demo like below:

    Model:

    public class User
    {
        [Key]
        public int UserID { get; set; }
        public int DefaultDepot { get; set; }
        public int DefaultDepartment { 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; }
    }

    Controller:

    public IEnumerable<SelectListItem> GetLocationsByDepotForDropdown()
    {
        int userId = 2;
        int[] depotNo = { 1, 2 };
        List<SelectListItem> locations = (from dpt in _context.goDepartment
                                            join dep in _context.goUserDepartment
                                            on dpt.DepartmentID equals dep.DepartmentID
                                            join user in _context.User
                                            on dep.UserID equals user.UserID 
                                            where dep.UserID == userId
                                            &&  depotNo.Contains(dep.DepotNo)
                                            && dpt.IsDeleted == false
                                            select new SelectListItem
                                            {
                                                Value = dpt.DepartmentID.ToString(),
                                                Text = dpt.DepartmentName,
                                                Selected = depotNo.Contains(user.DefaultDepot) && dpt.DepartmentID == user.DefaultDepartment
                                            }).ToList<SelectListItem>();
        locations.Insert(0, new SelectListItem
        {
            Value = "0",
            Text = "-- Select --"
        });
        return locations;
    }

    Result:

    Best Regards,

    Rena

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, August 4, 2020 8:40 AM

All replies

  • User711641945 posted

    Hi polachan,

    Please share your whole model when you ask the questions otherwise we would waste much time building models for your issue.The different model would make different results.

    Here is a working demo like below:

    Model:

    public class User
    {
        [Key]
        public int UserID { get; set; }
        public int DefaultDepot { get; set; }
        public int DefaultDepartment { 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; }
    }

    Controller:

    public IEnumerable<SelectListItem> GetLocationsByDepotForDropdown()
    {
        int userId = 2;
        int[] depotNo = { 1, 2 };
        List<SelectListItem> locations = (from dpt in _context.goDepartment
                                            join dep in _context.goUserDepartment
                                            on dpt.DepartmentID equals dep.DepartmentID
                                            join user in _context.User
                                            on dep.UserID equals user.UserID 
                                            where dep.UserID == userId
                                            &&  depotNo.Contains(dep.DepotNo)
                                            && dpt.IsDeleted == false
                                            select new SelectListItem
                                            {
                                                Value = dpt.DepartmentID.ToString(),
                                                Text = dpt.DepartmentName,
                                                Selected = depotNo.Contains(user.DefaultDepot) && dpt.DepartmentID == user.DefaultDepartment
                                            }).ToList<SelectListItem>();
        locations.Insert(0, new SelectListItem
        {
            Value = "0",
            Text = "-- Select --"
        });
        return locations;
    }

    Result:

    Best Regards,

    Rena

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, August 4, 2020 8:40 AM
  • User-1355965324 posted

    many many thanks

    Tuesday, August 4, 2020 6:13 PM