设置值以在另一个列表中列出

时间:2018-06-25 14:00:09

标签: c# list object syntax

MyItems模型类包含MyItemGallery项目的列表。我能够循环通过每个MyItems来增加价值,但是问题是我不知道如何为MyItems内的MyItemGallery增加价值?您能否提供一个示例,以便我了解如何为MyItemGallery设置值?

控制器:

List<MyItems> MyItemList = new List<MyItems>();

Items //Items contains list of some items data
foreach (var Item in Items)
{
    MyItemList.Add(new MyItems {
        EbayID = 1,
        Title = "some title",
        UserStoresID = 20
    });
}

MyItems.cs:

namespace Test_Console
{
    class MyItems
    {

        public string EbayID { get; set; }

        public string Title { get; set; }

        public int UserStoresID { get; set; }

        public List<MyItemGallery> MyItemGallery { get; set; }
    }

    class MyItemGallery
    {
        public int ItemID { get; set; }
        public string GalleryURL { get; set; }

    }
}

1 个答案:

答案 0 :(得分:1)

尚不清楚100%真正要实现的目标,但是从字面意义上来说,在创建主项目时,可以使用已经采用的相同对象创建模式轻松地将一个项目添加到库中创建并填充您的MyItem实例:

List<MyItems> MyItemList = new List<MyItems>();

Items //Items contains list of some items data
foreach (var Item in Items)
{
    MyItemList.Add(new MyItems {
        EbayID = 1,
        Title = "some title",
        UserStoresID = 20,
        MyItemGallery = new List<MyItemGallery>() { 
         new MyItemGallery() { ItemId = 1, GalleryURL = "http://www.example.com/gallery" } 
       }
    });
}