假设我有一个这样的课程
public class Product
{
public string Name {get; set;}
public string Category {get; set;}
public int ID {get; set;}
}
我也有这样的课程
[DataObject]
public class Products
{
[DataObjectMethod(DataObjectMethodType.Select)]
public IEnumerable<Product> GetProducts()
{
return new List<Product>
{
new Product() {Name = "Product1", Category="category1", ID=1 },
new Product() {Name = "Product2", Category="category2", ID=2 },
new Product() {Name = "Product3", Category="category3", ID=3 },
new Product() {Name = "Product4", Category="category4", ID=4 },
};
}
}
如果我有10个Product类实例,我如何将这些实例中包含的信息添加到我的列表中?
示例:
[DataObjectMethod(DataObjectMethodType.Select)]
public IEnumerable<Product> GetProducts()
{
return new List<Product>
{
new Product() {Name = "Instance1_Name", Category="Instance1_Category", ID=Instance1_ID },
new Product() {Name = "Instance2_Name", Category="Instance2_Category", ID=Instance2_ID },
new Product() {Name = "Instance3_Name", Category="Instance3_Category", ID=Instance3_ID },
new Product() {Name = "Instance4_Name", Category="Instance4_Category", ID=Instance4_ID },
};
}
这是我试图从Telerik使用的报告解决方案,它使用对象数据源在报告上生成内容。
答案 0 :(得分:0)
感谢此帖子的帮助,我得出了解决方案:C# how can I preserve data between class instances?
以下是最终代码示例:
public class Product
{
public string Name {get; set;}
public string Category {get; set;}
public int ID {get; set;}
public static List<Product> ProductList =
new List<Product>();
[DataObjectMethod(DataObjectMethodType.Select)]
public IEnumerable<Product> GenerateReport()
{
return ProductList;
}
}
关键是要结合我之前的两个不同的类,并使List成为静态成员来保存数据。