[英]Add property to list of a class
我有一个OrdersInfo的列表,我将OrdersInfo继承到YearlyResourceReport中并添加了12个属性-months(公共字符串Jan {get; set;})等
现在,我创建新类的列表(因此[OldProperties] + [12Month])
如何将两个列表合并在一起?
class YearlyResourceReport : OrdersInfo
{
public string Jan { get; set; }
public string Feb { get; set; }
public string Mar { get; set; }
public string Apr { get; set; }
public string Jun { get; set; }
public string Jul { get; set; }
public string Aug { get; set; }
public string Sep { get; set; }
public string Oct { get; set; }
public string Nov { get; set; }
public string Dec { get; set; }
}
加成:
List<OrdersInfo> ordersList =
WorkOrderEntity.GetYearlyOrders(year, loggedInUser, customerIds, sortBy).ToList();
List<YearlyResourceReport> newList;
我希望将属性添加到第一个列表(根本不必创建第二个列表),或者作为最后一个措施将两个列表合并。
这应该作为一个示例,使您走正确的道路:
class A
{
public string PropertyA { get; set; }
public override string ToString() { return this.PropertyA; }
}
class B : A
{
public string PropertyB { get; set; }
public override string ToString() { return string.Format("{0} - {1}", this.PropertyA, this.PropertyB); }
}
var aList = new List<A>();
var bList = new List<B>();
for (int i = 0; i < 10; i++)
{
aList.Add(new A() { PropertyA = string.Format("A - {0}", i) });
bList.Add(new B() { PropertyA = string.Format("B::A - {0}", i), PropertyB = string.Format("B::B - {0}", i) });
}
// now list the current state of the two lists
for (int i = 0; i < 10; i++)
{
Console.WriteLine(aList[i]);
Console.WriteLine(bList[i]);
}
Console.WriteLine();
Console.WriteLine();
// now merge the lists and print that result
var newList = bList.Concat(aList.Select(a => new B() { PropertyA = a.PropertyA, PropertyB = "Created by system." }));
foreach (var item in newList)
{
Console.WriteLine(item);
}
在上面的示例中,我有一个类A
和B
而B
从A
继承并添加了一个属性。 然后,我建立两者的清单,并将它们写到Console
。 之后,我们合并两个列表,在A
创建B
,因为单个通用列表必须具有相同的类型。 可以想象,可以使用ArrayList
类的东西来代替泛型列表,并容纳两种不同的类型-但我认为这不是您想要的。
合并类型后,我们还将合并结果写入Console
,您会看到两个列表已合并。
如果您的要求有所不同,那么您将获得90%的收益,因为老实说您的问题所包含的信息并不多,所以您让我们来承担一些责任。
注意:我使用scriptcs
编写scriptcs
来编译,运行和证明此示例,所以这就是为什么它不那么结构化的原因。
如果我确实正确理解了您的问题,那么您想这样做。
// Your class definitions
public class YearlyResourceReport
{
public YearlyResourceReport()
{
this.MonthlyResourceReports = new List<MonthlyOrderInfo>();
}
public List<MonthlyOrderInfo> MonthlyResourceReports { get; set; }
}
public class MonthlyOrderInfo
{
public string Month { get; set; }
public int MonthNumber { get; set; }
public OrdersInfo OrdersInfo { get; set; }
}
public class OrdersInfo
{
public int Id { get; set; }
public string description { get; set; }
public int TotalOrders { get; set; }
public double TotalRevenue { get; set; }
}
您可以创建包含YearlyResourceReport类,如下所示:
public YearlyResourceReport GetYearlyOrders()
{
YearlyResourceReport yrr = new YearlyResourceReport();
for(int i = 1; i <= 12; i++)
{
yrr.MonthlyResourceReports.Add(new MonthlyOrderInfo {
moi.MonthNumber = i,
moi.OrdersInfo = WorkOrderEntity.GetMonthlyOrders(year, i, loggedInUser, customerIds, sortBy).ToList()
});
}
return result;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.