我正在使用ASP.NET和C#在gridview中创建一个比较表。我在每个表单元格或每行上渲染数据。还有其他方法可以将3个或更多列表组合或合并为一个列表吗?
因此,到目前为止,我使用.Concat方法扩展名,但它只是添加到集合中,而不是插入到列表的索引中。
var input = new List<InventoryItem>();
for (var i = 0; i < inventoryItems.Count; i++)
{
if (i != 0)
{
var list = inventoryItems[0].Concat(inventoryItems[i]);
}
}
for (var day = 0; day <= 31; day++)
{
// Loop the Output by Day Index
if (day != 0)
{
var label = new Label();
label.Text = JsonConvert.SerializeObject(input);
e.Row.Cells[day].Controls.Add(label);
}
}
public class InventoryItem
{
public DateTime Date {get;set;}
public Name string {get;set;}
public Price decimal {get;set}
public Description string {get;set}
}
样本数据:
[JANUARY]
inventoryItems - Count = 3
[0] - Count = 31
[1]
- Date: {1/1/2019}
- Name: "Salad"
- Price: 10.00
- Description: "This Salad Description"
[2]
- Date: {1/2/2019}
- Name: "Salad"
- Price: 10.00
- Description: "This Salad Description"
[3]
- Date: {1/3/2019}
- Name: "Salad"
- Price: 10.00
- Description: "This Salad Description"
[4]
- Date: {1/4/2019}
- Name: "Salad"
- Price: 10.00
- Description: "This Salad Description"
[5]
- Date: {1/5/2019}
- Name: "Salad"
- Price: 10.00
- Description: "This Salad Description"
[6]
- Date: {1/6/2019}
- Name: "Salad"
- Price: 10.00
- Description: "This Salad Description"
[7]
- Date: {1/7/2019}
- Name: "Salad"
- Price: 10.00
- Description: "This Salad Description"
[8]
- Date: {1/8/2019}
- Name: "Salad"
- Price: 10.00
- Description: "This Salad Description"
[9]
- Date: {1/9/2019}
- Name: "Salad"
- Price: 10.00
- Description: "This Salad Description"
[10]
- Date: {1/10/2019}
- Name: "Salad"
- Price: 10.00
- Description: "This Salad Description"
[11]
- Date: {1/11/2019}
- Name: "Salad"
- Price: 10.00
- Description: "This Salad Description"
[12]
- Date: {1/12/2019}
- Name: "Salad"
- Price: 10.00
- Description: "This Salad Description"
[13]
- Date: {1/13/2019}
- Name: "Salad"
- Price: 10.00
- Description: "This Salad Description"
[14]
- Date: {1/14/2019}
- Name: "Salad"
- Price: 10.00
- Description: "This Salad Description"
[15]
- Date: {1/15/2019}
- Name: "Salad"
- Price: 10.00
- Description: "This Salad Description"
[16]
- Date: {1/16/2019}
- Name: "Salad"
- Price: 10.00
- Description: "This Salad Description"
[17]
- Date: {1/17/2019}
- Name: "Salad"
- Price: 10.00
- Description: "This Salad Description"
[18]
- Date: {1/18/2019}
- Name: "Salad"
- Price: 10.00
- Description: "This Salad Description"
[19]
- Date: {1/19/2019}
- Name: "Salad"
- Price: 10.00
- Description: "This Salad Description"
[20]
- Date: {1/20/2019}
- Name: "Salad"
- Price: 10.00
- Description: "This Salad Description"
[21]
- Date: {1/21/2019}
- Name: "Salad"
- Price: 10.00
- Description: "This Salad Description"
[22]
- Date: {1/22/2019}
- Name: "Salad"
- Price: 10.00
- Description: "This Salad Description"
[23]
- Date: {1/23/2019}
- Name: "Salad"
- Price: 10.00
- Description: "This Salad Description"
[24]
- Date: {1/24/2019}
- Name: "Salad"
- Price: 10.00
- Description: "This Salad Description"
[25]
- Date: {1/25/2019}
- Name: "Salad"
- Price: 10.00
- Description: "This Salad Description"
[26]
- Date: {1/26/2019}
- Name: "Salad"
- Price: 10.00
- Description: "This Salad Description"
[27]
- Date: {1/27/2019}
- Name: "Salad"
- Price: 10.00
- Description: "This Salad Description"
[28]
- Date: {1/28/2019}
- Name: "Salad"
- Price: 10.00
- Description: "This Salad Description"
[29]
- Date: {1/29/2019}
- Name: "Salad"
- Price: 10.00
- Description: "This Salad Description"
[30]
- Date: {1/30/2019}
- Name: "Salad"
- Price: 10.00
- Description: "This Salad Description"
[31]
- Date: {1/31/2019}
- Name: "Salad"
- Price: 10.00
- Description: "This Salad Description"
[1] - Count = 31
[1]
- Date: {1/1/2019}
- Name: "Salad"
- Price: 10.00
- Description: "This Salad Description"
[2]
- Date: {1/2/2019}
- Name: "Salad"
- Price: 10.00
- Description: "This Salad Description"
[3]
- Date: {1/3/2019}
- Name: "Salad"
- Price: 10.00
- Description: "This Salad Description"
[4]
- Date: {1/4/2019}
- Name: "Salad"
- Price: 10.00
- Description: "This Salad Description"
[5]
- Date: {1/5/2019}
- Name: "Salad"
- Price: 10.00
- Description: "This Salad Description"
[6]
- Date: {1/6/2019}
- Name: "Salad"
- Price: 10.00
- Description: "This Salad Description"
[7]
- Date: {1/7/2019}
- Name: "Salad"
- Price: 10.00
- Description: "This Salad Description"
[8]
- Date: {1/8/2019}
- Name: "Salad"
- Price: 10.00
- Description: "This Salad Description"
[9]
- Date: {1/9/2019}
- Name: "Salad"
- Price: 10.00
- Description: "This Salad Description"
[10]
- Date: {1/10/2019}
- Name: "Salad"
- Price: 10.00
- Description: "This Salad Description"
[11]
- Date: {1/11/2019}
- Name: "Salad"
- Price: 10.00
- Description: "This Salad Description"
[12]
- Date: {1/12/2019}
- Name: "Salad"
- Price: 10.00
- Description: "This Salad Description"
[13]
- Date: {1/13/2019}
- Name: "Salad"
- Price: 10.00
- Description: "This Salad Description"
[14]
- Date: {1/14/2019}
- Name: "Salad"
- Price: 10.00
- Description: "This Salad Description"
[15]
- Date: {1/15/2019}
- Name: "Salad"
- Price: 10.00
- Description: "This Salad Description"
[16]
- Date: {1/16/2019}
- Name: "Salad"
- Price: 10.00
- Description: "This Salad Description"
[17]
- Date: {1/17/2019}
- Name: "Salad"
- Price: 10.00
- Description: "This Salad Description"
[18]
- Date: {1/18/2019}
- Name: "Salad"
- Price: 10.00
- Description: "This Salad Description"
[19]
- Date: {1/19/2019}
- Name: "Salad"
- Price: 10.00
- Description: "This Salad Description"
[20]
- Date: {1/20/2019}
- Name: "Salad"
- Price: 10.00
- Description: "This Salad Description"
[21]
- Date: {1/21/2019}
- Name: "Salad"
- Price: 10.00
- Description: "This Salad Description"
[22]
- Date: {1/22/2019}
- Name: "Salad"
- Price: 10.00
- Description: "This Salad Description"
[23]
- Date: {1/23/2019}
- Name: "Salad"
- Price: 10.00
- Description: "This Salad Description"
[24]
- Date: {1/24/2019}
- Name: "Salad"
- Price: 10.00
- Description: "This Salad Description"
[25]
- Date: {1/25/2019}
- Name: "Salad"
- Price: 10.00
- Description: "This Salad Description"
[26]
- Date: {1/26/2019}
- Name: "Salad"
- Price: 10.00
- Description: "This Salad Description"
[27]
- Date: {1/27/2019}
- Name: "Salad"
- Price: 10.00
- Description: "This Salad Description"
[28]
- Date: {1/28/2019}
- Name: "Salad"
- Price: 10.00
- Description: "This Salad Description"
[29]
- Date: {1/29/2019}
- Name: "Salad"
- Price: 10.00
- Description: "This Salad Description"
[30]
- Date: {1/30/2019}
- Name: "Salad"
- Price: 10.00
- Description: "This Salad Description"
[31]
- Date: {1/31/2019}
- Name: "Salad"
- Price: 10.00
- Description: "This Salad Description"
[2] - Count = 31
[1]
- Date: {1/1/2019}
- Name: "Salad"
- Price: 10.00
- Description: "This Salad Description"
[2]
- Date: {1/2/2019}
- Name: "Salad"
- Price: 10.00
- Description: "This Salad Description"
[3]
- Date: {1/3/2019}
- Name: "Salad"
- Price: 10.00
- Description: "This Salad Description"
[4]
- Date: {1/4/2019}
- Name: "Salad"
- Price: 10.00
- Description: "This Salad Description"
[5]
- Date: {1/5/2019}
- Name: "Salad"
- Price: 10.00
- Description: "This Salad Description"
[6]
- Date: {1/6/2019}
- Name: "Salad"
- Price: 10.00
- Description: "This Salad Description"
[7]
- Date: {1/7/2019}
- Name: "Salad"
- Price: 10.00
- Description: "This Salad Description"
[8]
- Date: {1/8/2019}
- Name: "Salad"
- Price: 10.00
- Description: "This Salad Description"
[9]
- Date: {1/9/2019}
- Name: "Salad"
- Price: 10.00
- Description: "This Salad Description"
[10]
- Date: {1/10/2019}
- Name: "Salad"
- Price: 10.00
- Description: "This Salad Description"
[11]
- Date: {1/11/2019}
- Name: "Salad"
- Price: 10.00
- Description: "This Salad Description"
[12]
- Date: {1/12/2019}
- Name: "Salad"
- Price: 10.00
- Description: "This Salad Description"
[13]
- Date: {1/13/2019}
- Name: "Salad"
- Price: 10.00
- Description: "This Salad Description"
[14]
- Date: {1/14/2019}
- Name: "Salad"
- Price: 10.00
- Description: "This Salad Description"
[15]
- Date: {1/15/2019}
- Name: "Salad"
- Price: 10.00
- Description: "This Salad Description"
[16]
- Date: {1/16/2019}
- Name: "Salad"
- Price: 10.00
- Description: "This Salad Description"
[17]
- Date: {1/17/2019}
- Name: "Salad"
- Price: 10.00
- Description: "This Salad Description"
[18]
- Date: {1/18/2019}
- Name: "Salad"
- Price: 10.00
- Description: "This Salad Description"
[19]
- Date: {1/19/2019}
- Name: "Salad"
- Price: 10.00
- Description: "This Salad Description"
[20]
- Date: {1/20/2019}
- Name: "Salad"
- Price: 10.00
- Description: "This Salad Description"
[21]
- Date: {1/21/2019}
- Name: "Salad"
- Price: 10.00
- Description: "This Salad Description"
[22]
- Date: {1/22/2019}
- Name: "Salad"
- Price: 10.00
- Description: "This Salad Description"
[23]
- Date: {1/23/2019}
- Name: "Salad"
- Price: 10.00
- Description: "This Salad Description"
[24]
- Date: {1/24/2019}
- Name: "Salad"
- Price: 10.00
- Description: "This Salad Description"
[25]
- Date: {1/25/2019}
- Name: "Salad"
- Price: 10.00
- Description: "This Salad Description"
[26]
- Date: {1/26/2019}
- Name: "Salad"
- Price: 10.00
- Description: "This Salad Description"
[27]
- Date: {1/27/2019}
- Name: "Salad"
- Price: 10.00
- Description: "This Salad Description"
[28]
- Date: {1/28/2019}
- Name: "Salad"
- Price: 10.00
- Description: "This Salad Description"
[29]
- Date: {1/29/2019}
- Name: "Salad"
- Price: 10.00
- Description: "This Salad Description"
[30]
- Date: {1/30/2019}
- Name: "Salad"
- Price: 10.00
- Description: "This Salad Description"
[31]
- Date: {1/31/2019}
- Name: "Salad"
- Price: 10.00
- Description: "This Salad Description"
[FEBRUARY]
inventoryItems - Count = 3
[0] - Count = 28
[1]
- Date: {2/1/2019}
- Name: "Gelatin"
- Price: 10.00
- Description: "This Gelatin Description"
[2]
- Date: {2/2/2019}
- Name: "Gelatin"
- Price: 10.00
- Description: "This Gelatin Description"
[3]
- Date: {2/3/2019}
- Name: "Gelatin"
- Price: 10.00
- Description: "This Gelatin Description"
[4]
- Date: {2/4/2019}
- Name: "Gelatin"
- Price: 10.00
- Description: "This Gelatin Description"
[5]
- Date: {2/5/2019}
- Name: "Gelatin"
- Price: 10.00
- Description: "This Gelatin Description"
[6]
- Date: {2/6/2019}
- Name: "Gelatin"
- Price: 10.00
- Description: "This Gelatin Description"
[7]
- Date: {2/7/2019}
- Name: "Gelatin"
- Price: 10.00
- Description: "This Gelatin Description"
[8]
- Date: {2/8/2019}
- Name: "Gelatin"
- Price: 10.00
- Description: "This Gelatin Description"
[9]
- Date: {2/9/2019}
- Name: "Gelatin"
- Price: 10.00
- Description: "This Gelatin Description"
[10]
- Date: {2/10/2019}
- Name: "Gelatin"
- Price: 10.00
- Description: "This Gelatin Description"
[11]
- Date: {2/11/2019}
- Name: "Gelatin"
- Price: 10.00
- Description: "This Gelatin Description"
[12]
- Date: {2/12/2019}
- Name: "Gelatin"
- Price: 10.00
- Description: "This Gelatin Description"
[13]
- Date: {2/13/2019}
- Name: "Gelatin"
- Price: 10.00
- Description: "This Gelatin Description"
[14]
- Date: {2/14/2019}
- Name: "Gelatin"
- Price: 10.00
- Description: "This Gelatin Description"
[15]
- Date: {2/15/2019}
- Name: "Gelatin"
- Price: 10.00
- Description: "This Gelatin Description"
[16]
- Date: {2/16/2019}
- Name: "Gelatin"
- Price: 10.00
- Description: "This Gelatin Description"
[17]
- Date: {2/17/2019}
- Name: "Gelatin"
- Price: 10.00
- Description: "This Gelatin Description"
[18]
- Date: {2/18/2019}
- Name: "Gelatin"
- Price: 10.00
- Description: "This Gelatin Description"
[19]
- Date: {2/19/2019}
- Name: "Gelatin"
- Price: 10.00
- Description: "This Gelatin Description"
[20]
- Date: {2/20/2019}
- Name: "Gelatin"
- Price: 10.00
- Description: "This Gelatin Description"
[21]
- Date: {2/21/2019}
- Name: "Gelatin"
- Price: 10.00
- Description: "This Gelatin Description"
[22]
- Date: {2/22/2019}
- Name: "Gelatin"
- Price: 10.00
- Description: "This Gelatin Description"
[23]
- Date: {2/23/2019}
- Name: "Gelatin"
- Price: 10.00
- Description: "This Gelatin Description"
[24]
- Date: {2/24/2019}
- Name: "Gelatin"
- Price: 10.00
- Description: "This Gelatin Description"
[25]
- Date: {2/25/2019}
- Name: "Gelatin"
- Price: 10.00
- Description: "This Gelatin Description"
[26]
- Date: {2/26/2019}
- Name: "Gelatin"
- Price: 10.00
- Description: "This Gelatin Description"
[27]
- Date: {2/27/2019}
- Name: "Gelatin"
- Price: 10.00
- Description: "This Gelatin Description"
[28]
- Date: {2/28/2019}
- Name: "Gelatin"
- Price: 10.00
- Description: "This Gelatin Description"
[1] - Count = 28
[1]
- Date: {2/1/2019}
- Name: "Gelatin"
- Price: 10.00
- Description: "This Gelatin Description"
[2]
- Date: {2/2/2019}
- Name: "Gelatin"
- Price: 10.00
- Description: "This Gelatin Description"
[3]
- Date: {2/3/2019}
- Name: "Gelatin"
- Price: 10.00
- Description: "This Gelatin Description"
[4]
- Date: {2/4/2019}
- Name: "Gelatin"
- Price: 10.00
- Description: "This Gelatin Description"
[5]
- Date: {2/5/2019}
- Name: "Gelatin"
- Price: 10.00
- Description: "This Gelatin Description"
[6]
- Date: {2/6/2019}
- Name: "Gelatin"
- Price: 10.00
- Description: "This Gelatin Description"
[7]
- Date: {2/7/2019}
- Name: "Gelatin"
- Price: 10.00
- Description: "This Gelatin Description"
[8]
- Date: {2/8/2019}
- Name: "Gelatin"
- Price: 10.00
- Description: "This Gelatin Description"
[9]
- Date: {2/9/2019}
- Name: "Gelatin"
- Price: 10.00
- Description: "This Gelatin Description"
[10]
- Date: {2/10/2019}
- Name: "Gelatin"
- Price: 10.00
- Description: "This Gelatin Description"
[11]
- Date: {2/11/2019}
- Name: "Gelatin"
- Price: 10.00
- Description: "This Gelatin Description"
[12]
- Date: {2/12/2019}
- Name: "Gelatin"
- Price: 10.00
- Description: "This Gelatin Description"
[13]
- Date: {2/13/2019}
- Name: "Gelatin"
- Price: 10.00
- Description: "This Gelatin Description"
[14]
- Date: {2/14/2019}
- Name: "Gelatin"
- Price: 10.00
- Description: "This Gelatin Description"
[15]
- Date: {2/15/2019}
- Name: "Gelatin"
- Price: 10.00
- Description: "This Gelatin Description"
[16]
- Date: {2/16/2019}
- Name: "Gelatin"
- Price: 10.00
- Description: "This Gelatin Description"
[17]
- Date: {2/17/2019}
- Name: "Gelatin"
- Price: 10.00
- Description: "This Gelatin Description"
[18]
- Date: {2/18/2019}
- Name: "Gelatin"
- Price: 10.00
- Description: "This Gelatin Description"
[19]
- Date: {2/19/2019}
- Name: "Gelatin"
- Price: 10.00
- Description: "This Gelatin Description"
[20]
- Date: {2/20/2019}
- Name: "Gelatin"
- Price: 10.00
- Description: "This Gelatin Description"
[21]
- Date: {2/21/2019}
- Name: "Gelatin"
- Price: 10.00
- Description: "This Gelatin Description"
[22]
- Date: {2/22/2019}
- Name: "Gelatin"
- Price: 10.00
- Description: "This Gelatin Description"
[23]
- Date: {2/23/2019}
- Name: "Gelatin"
- Price: 10.00
- Description: "This Gelatin Description"
[24]
- Date: {2/24/2019}
- Name: "Gelatin"
- Price: 10.00
- Description: "This Gelatin Description"
[25]
- Date: {2/25/2019}
- Name: "Gelatin"
- Price: 10.00
- Description: "This Gelatin Description"
[26]
- Date: {2/26/2019}
- Name: "Gelatin"
- Price: 10.00
- Description: "This Gelatin Description"
[27]
- Date: {2/27/2019}
- Name: "Gelatin"
- Price: 10.00
- Description: "This Gelatin Description"
[28]
- Date: {2/28/2019}
- Name: "Gelatin"
- Price: 10.00
- Description: "This Gelatin Description"
[2] - Count = 28
[1]
- Date: {2/1/2019}
- Name: "Gelatin"
- Price: 10.00
- Description: "This Gelatin Description"
[2]
- Date: {2/2/2019}
- Name: "Gelatin"
- Price: 10.00
- Description: "This Gelatin Description"
[3]
- Date: {2/3/2019}
- Name: "Gelatin"
- Price: 10.00
- Description: "This Gelatin Description"
[4]
- Date: {2/4/2019}
- Name: "Gelatin"
- Price: 10.00
- Description: "This Gelatin Description"
[5]
- Date: {2/5/2019}
- Name: "Gelatin"
- Price: 10.00
- Description: "This Gelatin Description"
[6]
- Date: {2/6/2019}
- Name: "Gelatin"
- Price: 10.00
- Description: "This Gelatin Description"
[7]
- Date: {2/7/2019}
- Name: "Gelatin"
- Price: 10.00
- Description: "This Gelatin Description"
[8]
- Date: {2/8/2019}
- Name: "Gelatin"
- Price: 10.00
- Description: "This Gelatin Description"
[9]
- Date: {2/9/2019}
- Name: "Gelatin"
- Price: 10.00
- Description: "This Gelatin Description"
[10]
- Date: {2/10/2019}
- Name: "Gelatin"
- Price: 10.00
- Description: "This Gelatin Description"
[11]
- Date: {2/11/2019}
- Name: "Gelatin"
- Price: 10.00
- Description: "This Gelatin Description"
[12]
- Date: {2/12/2019}
- Name: "Gelatin"
- Price: 10.00
- Description: "This Gelatin Description"
[13]
- Date: {2/13/2019}
- Name: "Gelatin"
- Price: 10.00
- Description: "This Gelatin Description"
[14]
- Date: {2/14/2019}
- Name: "Gelatin"
- Price: 10.00
- Description: "This Gelatin Description"
[15]
- Date: {2/15/2019}
- Name: "Gelatin"
- Price: 10.00
- Description: "This Gelatin Description"
[16]
- Date: {2/16/2019}
- Name: "Gelatin"
- Price: 10.00
- Description: "This Gelatin Description"
[17]
- Date: {2/17/2019}
- Name: "Gelatin"
- Price: 10.00
- Description: "This Gelatin Description"
[18]
- Date: {2/18/2019}
- Name: "Gelatin"
- Price: 10.00
- Description: "This Gelatin Description"
[19]
- Date: {2/19/2019}
- Name: "Gelatin"
- Price: 10.00
- Description: "This Gelatin Description"
[20]
- Date: {2/20/2019}
- Name: "Gelatin"
- Price: 10.00
- Description: "This Gelatin Description"
[21]
- Date: {2/21/2019}
- Name: "Gelatin"
- Price: 10.00
- Description: "This Gelatin Description"
[22]
- Date: {2/22/2019}
- Name: "Gelatin"
- Price: 10.00
- Description: "This Gelatin Description"
[23]
- Date: {2/23/2019}
- Name: "Gelatin"
- Price: 10.00
- Description: "This Gelatin Description"
[24]
- Date: {2/24/2019}
- Name: "Gelatin"
- Price: 10.00
- Description: "This Gelatin Description"
[25]
- Date: {2/25/2019}
- Name: "Gelatin"
- Price: 10.00
- Description: "This Gelatin Description"
[26]
- Date: {2/26/2019}
- Name: "Gelatin"
- Price: 10.00
- Description: "This Gelatin Description"
[27]
- Date: {2/27/2019}
- Name: "Gelatin"
- Price: 10.00
- Description: "This Gelatin Description"
[28]
- Date: {2/28/2019}
- Name: "Gelatin"
- Price: 10.00
- Description: "This Gelatin Description"
[TO DECEMBER]
我希望得到以下结果:
inventoryItems - Count = 31
[0]
[0] - Date
- Name
- Price
- Description
[1]
- Date
- Name
- Price
- Description
[2]
- Date
- Name
- Price
- Description
[1]
[0] - Date
- Name
- Price
- Description
[1]
- Date
- Name
- Price
- Description
[2]
- Date
- Name
- Price
- Description
[2]
[0] - Date
- Name
- Price
- Description
[1]
- Date
- Name
- Price
- Description
[2]
- Date
- Name
- Price
- Description
[3]
[0] - Date
- Name
- Price
- Description
[1]
- Date
- Name
- Price
- Description
[2]
- Date
- Name
- Price
- Description
[4]
[0] - Date
- Name
- Price
- Description
[1]
- Date
- Name
- Price
- Description
[2]
- Date
- Name
- Price
- Description
...
...
...
[31]
[0] - Date
- Name
- Price
- Description
[1]
- Date
- Name
- Price
- Description
[2]
- Date
- Name
- Price
- Description
答案 0 :(得分:0)
因此,您有一系列的项目列表,其中每个列表都具有相同的长度。换句话说:一个外部列表,其中每个元素都是一个内部列表。每个内部列表的长度都相同。
作为输出,您想交换外部列表和内部列表。
这就像在其中切换索引的二维数组:因此,如果使用X [3] [2],则会从原始列表中获得元素[2] [3]。像是“转台”。
List<List<InventoryItem>> source = ...
List<List<InventoryItem>> swappedSource = SwapMySource(source);
InventoryItem theInventoryItemThatIWant = swappedSource[3][4];
如果仅使用InventoryItem [3] [4],则交换完整的源列表将极大地浪费处理能力。因此,明智的做法是创建一个SwappedList
类,该类仅在需要时才交换项目。
让我们通用一下,所以您也可以将SwappedList类用于其他类型
如果实现IList<List<T>>
,还必须实现ICollection<T>
和IEnumerable<T>
,您可能还需要IReadOnlyList<T>
和IReadOnlyCollection<T>
>
class SwappedList<T> : IList<IList<T>>,
// Only if desired; most functions will be one lined calls to IList<IList<T>>
IReadOnlyList<IReadOnlyList<T>>
ICollection<ICollection<T>>,
IReadOnlyCollection<IReadOnlyCollection<T>>
IEnumerable<IEnumerable<T>>
{
public IList<IList<T>> Source {get; set;}
... // TODO: implement
}
最重要的功能是交换。如果您要求索引[4] [3],则需要返回[3] [4]:
public T GetSwapped(int i, int j)
{
return this.Source[j][i];
}
public void SetSwapped(int i, int j, T value)
{
this.Source[j][i] = value;
}
考虑添加范围检查并根据需要抛出ArgumentOutOfRange。
现在要实现属性this[int index]
,您需要返回一个实现IList的对象。该对象应包含所有交换的Source对象。
private class SwappedOuterList<T> : IList<T> // TODO: IReadOnlyList<T> etc?
{
public IList<IList<T>> Source {get; set; }
public int OuterIndex{get; set;}
public T this[int index]
{
get {return this.Source.GetSwapped(this.OuterIndex, index);
set this.Source.SetSwapped(this.OuterIndex, index) = value;
}
因此,现在很容易在this
中实现SwappedList<T>
的get部分
public IList<T> this [int index]
{
get
{
return new SwappedOuterList<T>
{
Source = this.Source,
OuterIndex = index,
};
}
用法:
List<List<InventoryItem>> source = ...
var swappedSource = new SwappedSource<InventoryItem>{Source = source};
InventoryItem x = swappedSource[3][4];
x.Name = "Hello";
真正发生的是复制了指向源的指针。当您要求swappedSource[3]
时,将创建一个新对象:再次复制指向源的指针以及索引3。
[4]
在新创建的对象上执行:此函数调用GetSwapped(3, 4)
,返回source[4][3]
。分配仅作用于此返回对象的名称。
看看这有多有效:只复制一些指针和索引,而不创建仅使用一个元素的完整列表。
要实现IList<IList<T>>
,您需要实现更多功能。 Count
是微不足道的。 GetEnumerator<IList<T>>
有点困难:
public IEnumerator<IList<T>> GetEnumerator()
{
// all innerArrays have the same length:
int innerArrayLength = ...
for (int i=0; i<innerArrayLength; ++i)
yield return this[i];
}
在创建SwappedList类之后,您要做的就是为InventoryItem创建扩展功能。这样,您就可以像使用LINQ函数一样使用它:
public Static IList<IList<InventoryItem>> ToSwappedLists(this List<List<InventoryItem>> source)
{
return new SwappedList<InventoryItem>
{
Source = source,
}
}
用法:
var result = FetchMyInventoryItems
.Where(x => ...)
.ToSwappedList()
.GroupBy(...)
.Select(...)
仅当您不打算交换完整列表时,以上方法才有用。如果您确定将始终使用完整列表,则不必费心创建完整的SwappedList类,只需创建扩展功能即可创建完全交换的列表:
public Static List<List<InventoryItem>> ToSwappedLists(this List<List<InventoryItem>> source)
{
// TODO: check for null source
// TODO: check if all inner lists have same size
int innerListSize = ..
List<List<InventoryItem> result = new List<List<InventoryItem>>(innerListSize));
for (int y = 0; y<innerListsize; ++y)
{
var swappedList = this.Source.Select(x => source[x][y]).ToList();
result.Add(swappedList);
}
return result;
}
用法相同:
var result = FetchMyInventoryItems()
.ToSwappedList();