locked
how to filter list with linq using ef codefirst RRS feed

  • Question

  •   hi;

      I have a table structure like below

      MAINMENU table fields > ID,MAINMENUNAME , MENUCODE

          SUBMENU table fields > ID,SUBMENUNAME , SUBMENUCODE , PARENTCODE

          MENUAUTH table fields > TRCODE ,MENUID,USERID

      submenu join to mainmenu wirh parentcode ,submenu join to menuauth trcode=1 and menuid ,mainmenu join to menuauth trcode=0 and menuid

      also users table join to menuauth with userid,

      my code structure like below and everything is working fine.

      var db = new BulutContext();

          var mainMenus = db.Mainmenus.OrderBy(x => x.MAINMENUNAME).ToList();
          var subMenus = db.Submenus.OrderBy(x => x.SUBMENUNAME).ToList();

          foreach (var item in mainMenus)
          {
            item.MainMenus = new List<CloudSubMenu>();
            item.MainMenus.AddRange(subMenus.Where(x => x.PARENTCODE == item.MAINMENUCODE).ToList());

    --I am collect to submenu in mainmenu Classs
    --public List<CloudSubMenu> MainMenus { get; set; }

          }

      I want to filter menu for login users permission ,How can I do ? ,

      so , I assign the users permission MENUAUTH table , and I want to users only show if has a record in MENUAUTH table

                        
    • Edited by Aly14 Tuesday, April 21, 2020 12:29 AM b
    Tuesday, April 21, 2020 12:16 AM

All replies

  • Hi Aly14,
    Based on your description, do you want to filter the MENUAUTH table which joins to the users table with userid?
    If so, you can get two lists first and use linq to filter. Here is a thread you can refer to. 
    If not, please provide more detailed instructions.
    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, April 21, 2020 6:30 AM
  • I am processsing in below structure ,I want list and loop the table and filters

    var userId = Convert.ToInt32(cookUser["p4"]);

     var mainMenus = db.Mainmenus.OrderBy(x => x.MAINMENUNAME).ToList();   --mainmenu
           var subMenus = db.Submenus.OrderBy(x => x.SUBMENUNAME).ToList();           --submenu

           var mainMenuPermission = db.MenuAuths.Where(x => x.ACTIVE == 1 && x.USERID == userId  && x.TRCODE == 0).ToList();   

          var subMenuPermission = db.MenuAuths.Where(x => x.ACTIVE == 1 && x.USERID == userId  && x.TRCODE == 1).ToList();

    Tuesday, April 21, 2020 9:57 AM
  • Hi Aly14,
    >>var mainMenus = db.Mainmenus.OrderBy(x => x.MAINMENUNAME).ToList(); 
    I viewed your code, it is sorted by MAINMENUNAME and then converted into a list.
    >> var mainMenuPermission = db.MenuAuths.Where(x => x.ACTIVE == 1 && x.USERID == userId  && x.TRCODE == 0).ToList();  
    This statement states that it filters the data by specific conditions and then convert into a list.
    So what problem did you encounter?
    >>submenu join to mainmenu wirh parentcode
    What is the relationship between these two tables?
    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, April 22, 2020 8:50 AM
  • var mainMenus = db.Mainmenus.OrderBy(x => x.MAINMENUNAME).ToList();   --this is menümenü full list
           var subMenus = db.Submenus.OrderBy(x => x.SUBMENUNAME).ToList();           --this is submenü full list

           var mainMenuPermission = db.MenuAuths.Where(x => x.ACTIVE == 1 && x.USERID == userId  && x.TRCODE == 0).ToList();   this is ,user allowed the mainmenü with ID 

          var subMenuPermission = db.MenuAuths.Where(x => x.ACTIVE == 1 && x.USERID == userId  && x.TRCODE == 1).ToList(); this is ,user allowed the submenü with ID 

    I want to filter mainmenüs and submenüs only included users permission table

    main menü ID 1,2,3,4  submenü ID ,1,2,3,4,5,6,7,8,9,10

    users allowd main menü 2,3 , users allowed submenü , 1,4,6,7

    I want to filter users only access to main menü of 2,3 and submenü 1,4,6,7

    Wednesday, April 22, 2020 9:54 AM
  • Hi Aly14,
    I viewed your code, did you encounter an error in the above code?Or did you obtained the data and stored it in the list?
    If you have got the data, you can filter a list based on another list in Linq.
    And have you refer to this thread?
    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, April 23, 2020 9:50 AM
  • hi ;

    I gave enough information about the problem ,also review that you refer article ,if you know the answered that I asked , you may give a solution , otherwise you may ignore , I will wait someone

    Friday, April 24, 2020 10:46 AM