如何在 C# 中访问 class 的列表 <> 属性?
How to access a List<> property of a class in C#?
我基本上是在用 餐厅 信息填充中继器。
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();
我想要做的是,<table>
我想在其中显示我的转发器数据,我想显示与每个餐厅相关联的类别名称。
如何访问 List<Category>
的值?
我尝试过的:
遍历每个 Restaurant
项。将项目的值存储在 Category
:
类型的列表中
foreach(var restaurant in RestaurantData)
{
restaurant.instanceOfCategory. // properties of Category do not appear
}
我喜欢使用易于绑定的数据表。尝试以下:
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; }
}
}
我基本上是在用 餐厅 信息填充中继器。
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();
我想要做的是,<table>
我想在其中显示我的转发器数据,我想显示与每个餐厅相关联的类别名称。
如何访问 List<Category>
的值?
我尝试过的:
遍历每个 Restaurant
项。将项目的值存储在 Category
:
foreach(var restaurant in RestaurantData)
{
restaurant.instanceOfCategory. // properties of Category do not appear
}
我喜欢使用易于绑定的数据表。尝试以下:
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; }
}
}