是否有其他方法可以分组或合并分离的列表集合 并存储列表 到新列表的索引 ?

问题描述 投票:0回答:1

我正在使用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
c# linq
1个回答
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]时,会创建一个新对象:再次复制指向source的指针,以及索引3。

The[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();
© www.soinside.com 2019 - 2024. All rights reserved.