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

时间:2016-01-29 13:57:36

标签: c# sql-server linq stored-procedures lambda

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

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;
        }

我不知道如何更改最后一位,以便通过菜单列表查看,并根据usertype将菜单名称从“Admin”更改为“My Profile”。

1 个答案:

答案 0 :(得分:1)

如果我理解你,这是一种方式。

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

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

匹配的元素加入到该结果中