如何将项添加到另一个列表中的列表中。并使用条件查找另一个列表

时间:2015-03-26 06:36:24

标签: c# entity-framework

namespace Winning.FrameWork.PageMenuSetting
{
   public static class PreviewMenuHelper
    {

       /// <summary>
       /// 预览数据返回类型
       /// </summary>
       public  class TagPageInfo
       {
           public string TabPageName { get; set; }
           public List<MenuGroupInfo> ListMenuGroups { get; set; }

       }
       /// <summary>
       /// menugroup 返回类型
       /// </summary>
       public  class MenuGroupInfo
       {
           public string GroupId{ get; set; }
           public string GroupName{ get; set; }
           public List<MenuItemInfo> MenuItems{ get; set; }      

       }
       /// <summary>
       /// menuitem 返回类型
       /// </summary>
       public  class MenuItemInfo
       {
           public string CommandId { get; set; }
           public string AliasName { get; set; }
           public string ICON { get; set; }
           public int ShowMode { get; set; }
           public int ShowIndex { get; set; }

       }

       /// <summary>
       ///  获得所有的指定pageid的menu
       /// </summary>
       public static List<TagPageInfo> GetPageMenuInfo(string pageid)
       {
           List<TagPageInfo> listPage = new List<TagPageInfo>();
           TagPageInfo tagPageInfo = new TagPageInfo();     
           listPage.Add(tagPageInfo);
           listPage[0].TabPageName = pageid.Trim();           
           MenuGroupInfo menuGroupInfo = new MenuGroupInfo();          
           MenuItemInfo menuItemInfo = new MenuItemInfo();  
           int groupid = 1 ;
          var MenuItemsInfos = DataHelper.DataObj.QueryTable<PUB_MENU_ITEMINFO>(SystemType.H0, p => p.PAGEID.Trim() == pageid.Trim());
          listPage[0].ListMenuGroups = new List<MenuGroupInfo>();
          foreach (PUB_MENU_ITEMINFO item in MenuItemsInfos)
          {
              if (listPage[0].ListMenuGroups.Count<=0|| listPage[0].ListMenuGroups.FirstOrDefault(p => p.GroupName == item.MEMO) == null)
                  {
                      menuGroupInfo.GroupId = groupid.ToString();
                      menuGroupInfo.GroupName = item.MEMO;
                      menuItemInfo.CommandId = item.COMMANDID;
                      menuItemInfo.AliasName = item.MENUITEMNAME;
                      menuItemInfo.ICON = item.ICON;
                      menuItemInfo.ShowMode = 0;
                      menuItemInfo.ShowIndex = 1;
                      menuGroupInfo.MenuItems = new List<MenuItemInfo>();
                      menuGroupInfo.MenuItems.Add(menuItemInfo);
                      listPage[0].ListMenuGroups = new List<MenuGroupInfo>();
                      listPage[0].ListMenuGroups.Add(menuGroupInfo);
                      groupid++;
                  }
                  else
                  {
                      //var existgroup = listPage[0].ListMenuGroups.FirstOrDefault(t => t.GroupName == item.MEMO) as MenuGroupInfo;
                      menuItemInfo.AliasName=item.MENUITEMNAME;
                      menuItemInfo.CommandId=item.COMMANDID;
                      menuItemInfo.ShowMode = 0;
                      menuItemInfo.ShowIndex = 1;                        
                      //this is where the problem is. all items inside the list<menuiteminfo> become the same as the last menuiteminfo i insert into.
                      ((listPage[0].ListMenuGroups.FirstOrDefault(t => t.GroupName == item.MEMO) as MenuGroupInfo).MenuItems as List<MenuItemInfo>).Add(menuItemInfo);
                  }                

          }
          return listPage;

       }
    }
}

无法将新的menuiteminfo插入到一个存在的menugroupinfo中。 所有menuiteminfo成为我插入的最后一项,不知道为什么!请主要检查几行最后一行,指出我哪里出错!

1 个答案:

答案 0 :(得分:0)

MenuGroupInfo menuGroupInfo = new MenuGroupInfo();

MenuItemInfo menuItemInfo = new MenuItemInfo();

问题解决了,我定义了一个全球可怜的人。所以所有新的MenuItemInfo都添加到单个MenuGroupInfo中。