Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从一年内的月份列表中查找缺少的月份_C#_.net_Datetime - Fatal编程技术网

C# 如何从一年内的月份列表中查找缺少的月份

C# 如何从一年内的月份列表中查找缺少的月份,c#,.net,datetime,C#,.net,Datetime,我有下面的销售示例列表,其中显示了过去12个月的销售收入,其中2个月的销售收入可能或多或少 [{1, 400}, {2,500}, {4, 550}, {5, 425}, {6, 770}, {7, 500}, {9, 300}, {10, 900}, {11, 440}, {12, 620}] 月份是按升序排列的,因此1是1月,12是12月。如何在代码中更新此列表,以便为缺少的月份添加值为0的销售对象。在本例中,添加{3,0}和{8,0} 我从数据库中检索这些数据,其中一些数据缺失的原因是因

我有下面的销售示例列表,其中显示了过去12个月的销售收入,其中2个月的销售收入可能或多或少

[{1, 400}, {2,500}, {4, 550}, {5, 425}, {6, 770}, {7, 500}, {9, 300}, {10, 900}, {11, 440}, {12, 620}]
月份是按升序排列的,因此1是1月,12是12月。如何在代码中更新此列表,以便为缺少的月份添加值为0的销售对象。在本例中,添加{3,0}和{8,0}


我从数据库中检索这些数据,其中一些数据缺失的原因是因为它们在数据库中没有记录,所以我想为缺失的月份添加零。月份来自DateTime.Date.month。

假设您有一个表示月份的类:

public class MonthData
{
    public int Month { get; set; }
    public int Money { get; set; }
}
然后,您可以使用LINQ查找缺少的月份,然后将其插入原始列表:

var months = new List<MonthData>
{
    new MonthData {Month = 1, Money = 400},
    new MonthData {Month = 2, Money = 500},
    new MonthData {Month = 4, Money = 550},
    new MonthData {Month = 5, Money = 425},
    new MonthData {Month = 6, Money = 770},
    new MonthData {Month = 7, Money = 500},
    new MonthData {Month = 9, Money = 300},
    new MonthData {Month = 10, Money = 900},
    new MonthData {Month = 11, Money = 440},
    new MonthData {Month = 12, Money = 620}
};

# Find missing months
var missingMonths = Enumerable
    .Range(months.Min(m => m.Month), months.Max(m => m.Month))
    .Except(months.Select(m => m.Month))

# Insert missing months back into months list
foreach (var month in missingMonths)
{
    months.Insert(month - 1, new MonthData { Month = month, Money = 0 });
}
这需要ONLogN排序,因为字典本质上是无序的


试一试

假设您有一个代表您月份的课程:

public class MonthData
{
    public int Month { get; set; }
    public int Money { get; set; }
}
然后,您可以使用LINQ查找缺少的月份,然后将其插入原始列表:

var months = new List<MonthData>
{
    new MonthData {Month = 1, Money = 400},
    new MonthData {Month = 2, Money = 500},
    new MonthData {Month = 4, Money = 550},
    new MonthData {Month = 5, Money = 425},
    new MonthData {Month = 6, Money = 770},
    new MonthData {Month = 7, Money = 500},
    new MonthData {Month = 9, Money = 300},
    new MonthData {Month = 10, Money = 900},
    new MonthData {Month = 11, Money = 440},
    new MonthData {Month = 12, Money = 620}
};

# Find missing months
var missingMonths = Enumerable
    .Range(months.Min(m => m.Month), months.Max(m => m.Month))
    .Except(months.Select(m => m.Month))

# Insert missing months back into months list
foreach (var month in missingMonths)
{
    months.Insert(month - 1, new MonthData { Month = month, Money = 0 });
}
这需要ONLogN排序,因为字典本质上是无序的


试穿

试着尽可能靠近您的模型

// Example
var yearSales = new Dictionary<int, decimal>
{
    {  1, 100 },
    {  3, 300 },
    {  6, 600 },
};

// Actual logic
var fullMonths = Enumerable.Range(1, 12)
    .ToDictionary(
        month => month,
        month => yearSales.TryGetValue(month, out decimal d) ? d : default
    );

试着尽可能接近你的模型

// Example
var yearSales = new Dictionary<int, decimal>
{
    {  1, 100 },
    {  3, 300 },
    {  6, 600 },
};

// Actual logic
var fullMonths = Enumerable.Range(1, 12)
    .ToDictionary(
        month => month,
        month => yearSales.TryGetValue(month, out decimal d) ? d : default
    );

请向我们显示您本月保存列表的变量和结构。请向我们显示您本月保存列表的变量和结构。