【问题标题】:Add property to list of a class将属性添加到类列表
【发布时间】:2013-04-24 12:25:40
【问题描述】:

我有一个 OrdersInfo 列表 我将 OrdersInfo 继承到 YearlyResourceReport 并添加 12 个属性 - 月(公共字符串 Jan {get;set;})等

现在我创建一个新类的列表(So [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;

我希望将属性添加到第一个列表(根本不必创建第二个列表) 或者作为最后一项合并两个列表。

【问题讨论】:

  • 你的问题与标题有什么关系?
  • 以什么方式合并?问题不清楚。你想要的结果是什么?
  • 将属性添加到类列表中究竟是什么意思,您在 OrdersInfo 中是否有此类属性,并且您会看到重复的属性
  • 你想拥有 Jan,feb,... 属性的订单信息,所以在 Order info 类中声明它们,当你继承 YearlyResourceReport 时你得到了这些属性

标签: c# class inheritance properties


【解决方案1】:

这应该作为一个例子,让你走上正确的道路:

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 来编译、运行和证明这个例子,所以这就是它没有结构化的原因。

【讨论】:

    【解决方案2】:

    如果我确实正确理解了您的问题,您愿意这样做。

    // 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;
     }
    

    【讨论】:

      猜你喜欢
      • 2014-05-07
      • 2021-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多