向类的列表中添加属性

本文关键字:添加 属性 列表 | 更新日期: 2023-09-27 18:10:36

我有一个OrdersInfo列表我继承OrdersInfo到YearlyResourceReport和添加12个属性-月(公共字符串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);
}

在上面的例子中,我有一个类AB, B继承自A并添加了一个属性。然后构建两者的清单并将它们写入Console。之后,我们合并这两个列表,从A中创建B,因为单个泛型列表必须具有相同的类型。可以想象,你可以使用ArrayList之类的东西来代替通用列表,并容纳两种不同的类型——但我不认为这是你想要的。

一旦合并类型,我们将合并的结果也写入Console,您可以看到两个列表合并。

如果你的要求有点不同,这将使你超过90%的方式,因为老实说,你的问题并没有包含太多的信息,所以你让我们假设一些事情。

注意:我使用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;
 }