2013-04-24 105 views
1

我有一個OrdersInfo 的名單我繼承OrdersInfo到YearlyResourceReport,並添加12個性質 - 個月(公共字符串揚{獲得;設置;})等添加屬性一類的列表

現在我創建一個列表(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; 

我想無論是屬性添加到第一個列表(無需再創建第二個列表) 或者只是作爲最後措施合併兩個列表。

+0

哪裏列表? – 2013-04-24 12:27:25

+0

你的問題與標題的關係是什麼? – Obama 2013-04-24 12:27:27

+0

以什麼方式合併?問題不清楚。你期望的結果是什麼? – 2013-04-24 12:27:51

回答

4

這應該作爲一個例子,讓你去正確的道路:

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

在上面的例子中,我有一個類從AABB繼承,並增加了一個屬性。然後,我建立了兩者的列表並將它們寫出到Console。這樣做之後,我們再合併這兩個列表,創建BA's,因爲單個通用列表必須具有相同的類型。你可以想象使用類似於ArrayList而不是通用列表,並有兩種不同的類型 - 但我不認爲這就是你想要的。

一旦合併了類型,我們也會將該合併的結果寫入Console,並且您看到兩個列表已合併。

如果您的要求有一點不同,這將讓你90%以上的方式,因爲說實話你的問題沒有包含非常多的信息,從而給我們留下來樣承擔一些事情。

NOTE:我用scriptcs來編譯,運行並證明這個例子,所以這就是爲什麼它不是結構化的。

+1

精彩的解釋:) – saeed 2013-04-24 12:44:34

1

如果我確實瞭解你的問題,你想這樣做。

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