我将来自每月364天数据的列表集合进行分组,然后使用foreach进行循环。 然后我使用GroupBy按月对它们进行分组。 分组后,他们将分组12个月,并在一个月内列出30天。 但是我的问题是如何将12个月存储在一个列表中?
var inventories = new List<Month>
{
new Month { Id = 0, Name = "JAN", Inventories = new List<Inventory> { new Inventory { Id = 1, Name = "Can", Description = "Can Description" } } },
new Month { Id = 1, Name = "FEB", Inventories = new List<Inventory> { new Inventory { Id = 1, Name = "Can", Description = "Can Description" } } },
new Month { Id = 2, Name = "MAR", Inventories = new List<Inventory> { new Inventory { Id = 1, Name = "Can", Description = "Can Description" } } },
new Month { Id = 3, Name = "APR", Inventories = new List<Inventory> { new Inventory { Id = 1, Name = "Can", Description = "Can Description" } } },
new Month { Id = 4, Name = "MAY", Inventories = new List<Inventory> { new Inventory { Id = 1, Name = "Can", Description = "Can Description" } } },
new Month { Id = 5, Name = "JUN", Inventories = new List<Inventory> { new Inventory { Id = 1, Name = "Can", Description = "Can Description" } } },
new Month { Id = 6, Name = "JUL", Inventories = new List<Inventory> { new Inventory { Id = 1, Name = "Can", Description = "Can Description" } } },
new Month { Id = 7, Name = "AUG", Inventories = new List<Inventory> { new Inventory { Id = 1, Name = "Can", Description = "Can Description" } } },
new Month { Id = 8, Name = "SEP", Inventories = new List<Inventory> { new Inventory { Id = 1, Name = "Can", Description = "Can Description" } } },
new Month { Id = 9, Name = "OCT", Inventories = new List<Inventory> { new Inventory { Id = 1, Name = "Can", Description = "Can Description" } } },
new Month { Id = 10, Name = "NOV", Inventories = new List<Inventory> { new Inventory { Id = 1, Name = "Can", Description = "Can Description" } } },
new Month { Id = 12, Name = "DEC", Inventories = new List<Inventory> { new Inventory { Id = 1, Name = "Can", Description = "Can Description" }} },
};
var list = new List<Collection<Month>>();
// Currently I have 12 months that has a Inventories per month.
foreach (var month in inventories)
{
// Just store the 12 months in a single List<T>
// Example: [0] -> [12] - > [30] and so on.
}
class Month
{
public int Id { get; set; }
public string Name { get; set; }
public List<Inventory> Inventories { get; set; }
}
class Inventory
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
}
答案 0 :(得分:0)
这个问题尚不明确,但我认为您可以尝试使用此模型,而不是使用两个模型,而是可以通过分层实现的。
public class Inventory
{
public Inventory()
{
MonthList = new List<Month>();
}
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public List<Month> MonthList {get; set;}
}
public class Month
{
public Month()
{
DayList = new List<Day>();
}
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public List<Day> DayList {get; set;}
}
List<Inventory> inventoryList = new List<Inventory>()
{
new Inventory()
{
Id = 1,
Name = "test",
Description = "desc.",
MonthList = new List<Month>()
{
Id = 1,
Name = "test",
Description = "desc.",
DayList = new List<Day>(){...}
}
}
}
迭代结果
foreach(Inventory inv in inventoryList) //--> inv will be contains more than 3 list
{
inv.MonthList.... //--> it will be contains 12 months
foreach(Month month in inv.MonthList)
{
month.DayList... //--> will be contains 30 days
}
}