【问题标题】:How to access a List<> property of a class in C#?如何在 C# 中访问类的 List<> 属性?
【发布时间】:2020-01-16 17:35:53
【问题描述】:

我基本上是用餐厅信息填充中继器。

class Restaurant
{
    public int Id { get; set; };
    public string Name { get; set; };
    public List<Category> RestaurantCategory { get; set; };
    public string Address { get; set; };
}

class Category
{
    public int CategoryId { get; set; };
    public int RestaurantId { get; set; };
    public string CategoryName { get; set; };
}

我的中继器的名字是rptRestaurantInfo:

rptRestaurantInfo.DataSource = RestaurantData; //RestaurantData is of type List<Restaurant>
rptRestaurantInfo.DataBind();

我想做的是,我想在其中显示我的转发器数据的&lt;table&gt;,我想显示与每家餐厅关联的类别名称。

如何访问List&lt;Category&gt; 的值?

我尝试过的:

循环遍历每个 Restaurant 项目。将项目的值存储在 Category 类型的列表中:

    foreach(var restaurant in RestaurantData)
    {
        restaurant.instanceOfCategory. // properties of Category do not appear
    }

【问题讨论】:

    标签: c# asp.net list collections webforms


    【解决方案1】:

    我喜欢使用易于绑定的数据表。尝试以下:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                List<Restaurant> RestaurantData = new List<Restaurant>();
    
                DataTable rptRestaurantInfo = new DataTable();
                rptRestaurantInfo.Columns.Add("Id", typeof(int));
                rptRestaurantInfo.Columns.Add("Name", typeof(string));
                rptRestaurantInfo.Columns.Add("Address", typeof(string));
                rptRestaurantInfo.Columns.Add("CategoryId", typeof(int));
                rptRestaurantInfo.Columns.Add("RestaurantId", typeof(int));
                rptRestaurantInfo.Columns.Add("CategoryName", typeof(string));
    
                foreach(Restaurant restaurant in RestaurantData)
                {
                    foreach(Category category in restaurant.RestaurantCategory)
                    {
                        rptRestaurantInfo.Rows.Add(new object[] {
                            restaurant.Id,
                            restaurant.Name,
                            restaurant.Address,
                            category.CategoryId,
                            category.RestaurantId,
                            category.CategoryName
                        });
    
                    }
                }
            }
        }
        public class Restaurant
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public List<Category> RestaurantCategory { get; set; }
            public string Address { get; set; }
        }
    
        public class Category
        {
            public int CategoryId { get; set; }
            public int RestaurantId { get; set; }
            public string CategoryName { get; set; }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-27
      • 1970-01-01
      • 1970-01-01
      • 2017-04-15
      • 1970-01-01
      • 1970-01-01
      • 2016-01-29
      • 2016-12-06
      相关资源
      最近更新 更多