将 sql 存储过程转换为 Linq/Lambda

本文关键字:Linq Lambda 转换 sql 存储过程 | 更新日期: 2023-09-27 18:32:23

我有一个存储过程,它显示表菜单中的菜单,然后检查用户类型以根据用户类型更改菜单项的名称。

MenuID, 
    Case When @UserType <> 'E' and MenuName='Admin' then 'My Profile' When @UserType = 'A' and MenuName='Change Password' then 'Change Agent Password' else MenuName End As MenuName, 
    ParentID, 
    MenuLink,
    IconImagePath,
    MenuTarget 
from tblMenus          
Where IsDeleted=0 and 
      IsEnabled=1 and 
      MenuID in (Select MenuID From #MenuChild)                            
 Or MenuID In(SELECT ParentID FROM #MenuChild t CROSS APPLY dbo.FindRoot2(t.menuid))                            
 Or MenuID = 1  order by MenuOrder, MenuName    

我一直在努力将整个过程转换为 linq,现在看起来像这样。

 public List<tblMenu> getmainmenusclass()
        {  
             var UserInfo = GetUserInfo();
            UserType = UserInfo[0].UserTypeID;
            var menus = DataAccess.Menus.FindByExp(x => x.IsDeleted == false).ToList(); 
            if (UserType == "E")
                {
                    menus = DataAccess.Menus.FindByExp(x => x.IsDeleted == false).ToList();
                }
                if (UserType == "A")
                {
                    var agentDist = GetAgentDistribution();
                    Distribution = agentDist[0].AgtDistChannel;
                    if (Distribution == "P" || Distribution == "S")
                    {
                        menus = DataAccess.Menus.FindByExp(x => x.IsDeleted == false && x.MenuCategory != "DGB").ToList();
                    }
                    if (Distribution == "G" || Distribution == "B")
                    {
                        menus = DataAccess.Menus.FindByExp(x => x.IsDeleted == false && x.MenuCategory != "DPS").ToList();
                    }
                    if (Distribution == "W" || Distribution == "P")
                    {
                        menus = DataAccess.Menus.FindByExp(x => x.IsDeleted == false && x.MenuCategory != "DGB" && x.MenuName != "NEW Quoting Tool").ToList();
                    }
                }
                else
                {
                    var notAllowedMenuCategories = new[] { "DPS", "DGB", "D" };
                    menus = DataAccess.Menus.FindByExp(x => x.IsDeleted == false && !notAllowedMenuCategories.Contains(x.MenuCategory)).ToList();
                        if (Distribution == "G")
                    {
                        var notAllowedMenuCategoriesForG = new[] { "DPS", "DGB", "D", "PB" };
                        menus = DataAccess.Menus.FindByExp(x => x.IsDeleted == false && !notAllowedMenuCategoriesForG.Contains(x.MenuCategory)).ToList();
                    }
                }
                if (MarketingGroup != "BWCU" && MarketingGroup != "''")
                    {
                        menus = DataAccess.Menus.FindByExp(x => x.IsDeleted == false && x.MenuName != "NEW Quoting Tool").ToList();
                    }  
               return menus;
        }

我不确定如何更改最后一点,以便它浏览菜单列表,并根据用户类型将菜单名称从"管理员"更改为"我的个人资料"。

将 sql 存储过程转换为 Linq/Lambda

如果我完全

理解你,这是一种方式。

    menus = (from p in DataAccess.Menus 
               where p.usertype == 'A' 
              select new tblMenu 
             { //your properties except name
               Name="My Profile"
             }).Union(from p in DataAccess.Menus 
                  where p.usertype != "A" 
                  select p).ToList();

根据需要更改用户类型条件。在这个 linq 中,我从DataAccess.Menus中获取所有元素,如果用户类型为 A,那么我会按照您的要求更改一个属性。然后,我将该结果与所有其他不匹配的元素usertype