【问题标题】:How to add a number of records into a List<T>如何将多个记录添加到 List<T>
【发布时间】:2011-09-10 11:19:43
【问题描述】:

我使用实体框架创建了一个 asp.net 应用程序。在此我想将记录添加到列表中。为此,我必须使用 foreach 循环,但它总是只为所有记录添加最后一条记录数据,这意味着它显示相同的数据。在这里,我粘贴了我的代码。请验证一次并指导我可以更改的地方。

     public List<CategoryItems> ListMenuCategory(int MenuId)
    {
        string str = string.Empty;
        string strJSON = string.Empty;
        List<CategoryItems> resultmenu;
        resultmenu = new List<CategoryItems>();
        List<CategoryItems> Result;
        Result = new List<CategoryItems>();
        bool check = true;
        var objmenuCategory = from cat in objEntity.menucategories where cat.MenuId == MenuId && cat.Active == check select cat;
        CategoryItems Categorylist = new CategoryItems();
        foreach (menucategory category in objmenuCategory)
        {
            Categorylist.CategoryName = category.CategoryName;
            Categorylist.Description = category.Description;
            int menuid = category.MenuCategoryId;
            List<menuitem> menuitems = GetMenucategories(menuid);
            foreach (var items in menuitems)
            {
                Categorylist.ItemName = items.ItemName;
                Categorylist.Description = items.Description;
                Categorylist.Price = (float)items.Price;
                string Image = items.Picture;
                Categorylist.Picture = "http://restaurantmanager.testshell.net/Images/" + Image;
                Categorylist.Thumbnail = "http://restaurantmanager.testshell.net/Images/" + items.Thumbnail;
                if (items.CreatedDate != null)
                {
                    Categorylist.CreatedDate = (DateTime)items.CreatedDate;
                }

                if (items.ModifiedDate != null)
                {
                    Categorylist.ModifiedDate = (DateTime)items.ModifiedDate;
                }
                Result.Add(Categorylist);

            }


            // Result.AddRange(menus);


        }
        return Result;



        }


    private List<menuitem> GetMenucategories(int p)
       {
        restaurantEntities objEntity1 = new restaurantEntities();
         var menuitems = from items in objEntity1.menuitems where items.MenuCategoryId == p select items;
   return menuitems.ToList();
     }

【问题讨论】:

  • 因为这根本不是关于ArrayList,而是List&lt;T&gt;,所以我在问题中改变了它。
  • 谢谢你,你说得对

标签: c# asp.net list c#-4.0


【解决方案1】:

您正在循环之外创建 Categorylist 项目,因此您只使用一个项目,用不同的数据填充它并一遍又一遍地将其添加到结果中。

您必须在最里面的循环中创建项目,以便每次迭代都有自己的对象。


注意:ChrisF 还发现您在循环中调用了AddRange,这导致您将一遍又一遍地添加相同的项目集。你根本不需要调用AddRange,你可以完全跳过Result列表,直接返回resultmenu

【讨论】:

  • 您好,谢谢您的回复,实际上我想以 json 格式返回单个列表中的数据,我想显示 .... 之类的数据。 ..
  • 但它显示为 ......我在内部循环中使用项目对象 ....
  • @Victor:那么您必须更改您的对象,以便CategoryItem 类包含Items 集合,而不是单个项目。然后,您可以创建一个 CategoryItem 对象并将项目添加到其 Item 集合中。
  • 嗨,我已经使用类别 id 获取子表并将这些项目分配给 categoryitems 集合,但是如果子表中只有一条记录,则它显示结果正确,如果有多个记录它是列表项不在同一个列表中...
  • @Victor:是的,那是因为您有一个 CategoryItem 列表,每个列表包含一个项目,而不是一个 CategoryItem 包含项目列表。
猜你喜欢
  • 2021-01-21
  • 1970-01-01
  • 2020-11-09
  • 1970-01-01
  • 2014-04-14
  • 2020-12-07
  • 2019-07-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多