简体   繁体   English

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

[英]Converting sql stored procedure to Linq/Lambda

I have a stored procedure that displays a menu from table menus and then checks the user type to change the name of a menu item depending on the User type. 我有一个存储过程,该过程显示表菜单中的菜单,然后检查用户类型,以根据用户类型更改菜单项的名称。

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    

I have been working on converting the entire procedure to linq and it looks like this now. 我一直在将整个过程转换为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;
        }

I am not sure how to change that last bit so that it looks through the menu list and depending on the usertype changes the menu name from "Admin" to "My Profile". 我不确定如何更改最后一位,以便通过菜单列表进行查找,并且根据用户类型将菜单名称从“ Admin”更改为“ My Profile”。

If i understood you corectly, this is one way. 如果我完全了解您,那是一种方法。

    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();

Change the usertype condition as you require. 根据需要更改用户类型条件。 In this linq i'm taking all the elements from DataAccess.Menus and if the user type is A then i change one property as you requested. 在此linq中,我将从DataAccess.Menus获取所有元素,如果用户类型为A,则可以根据您的要求更改一个属性。 Then i'm joining that result with all the other elements that didn't match that usertype 然后,我将该结果与该用户类型不匹配的所有其他元素结合usertype

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM