Asp.net 使用LINQ将多个日期转换为范围

Asp.net 使用LINQ将多个日期转换为范围,asp.net,vb.net,linq,range,date-range,Asp.net,Vb.net,Linq,Range,Date Range,我需要帮助跨日历年应用设置。我的用户根据日历中的季节将数字目标输入数据库。想想看,苹果酒在秋季(9月至11月)的销量会更高,而在今年剩余时间(12月至8月)的销量会更低。然而,苹果汁一年四季的销量都很均衡 为了简化我的用户的数据输入,他们输入一个目标销售数字以及每个产品的起始月份 例如,数据库中的数据如下所示(任何产品最多有两个目标): 我在数据库表中也有我们的财政日历,因此我知道什么财政年度、月份、月份中的周数等与任何一天相关 我需要将此数据应用于报表,但我需要一年中每个月的数据,而不仅仅是用

我需要帮助跨日历年应用设置。我的用户根据日历中的季节将数字目标输入数据库。想想看,苹果酒在秋季(9月至11月)的销量会更高,而在今年剩余时间(12月至8月)的销量会更低。然而,苹果汁一年四季的销量都很均衡

为了简化我的用户的数据输入,他们输入一个目标销售数字以及每个产品的起始月份

例如,数据库中的数据如下所示(任何产品最多有两个目标):

我在数据库表中也有我们的财政日历,因此我知道什么财政年度、月份、月份中的周数等与任何一天相关

我需要将此数据应用于报表,但我需要一年中每个月的数据,而不仅仅是用户输入的数据

我在使用LINQ加入我的财务日历以创建数据分解视图时遇到问题?如何从一年中所有月份的列表开始,针对每种产品,将目标放在日历中,然后填入空格。例如,使用上述示例的苹果酒

January
February
March
April 
May 
June 
July 
August 
September - 5,000 gallonws
October
November 
December - 1,000 gallons


Apple Cider - September - 5,000 gallons
Apple Cider - October - 5,000 gallons
Apple Cider - November - 5,000 gallons
Apple Cider - December - 1,000 gallons
Apple Cider - January - 1,000 gallons
Apple Cider - February- 1,000 gallons
Apple Cider - March - 1,000 gallons
Apple Cider - April- 1,000 gallons
Apple Cider - May - 1,000 gallons
Apple Cider - June - 1,000 gallons
Apple Cider - July - 1,000 gallons
Apple Cider - August - 1,000 gallons

Apple Juice - September - 3,000 gallons
Apple Juice - October - 3,000 gallons
Apple Juice - November - 3,000 gallons
Apple Juice - December - 3,000 gallons
Apple Juice - January - 3,000 gallons
Apple Juice - February - 3,000 gallons
Apple Juice - March - 3,000 gallons
Apple Juice - April - 3,000 gallons
Apple Juice - May - 3,000 gallons
Apple Juice - June - 3,000 gallons
Apple Juice - July - 3,000 gallons
Apple Juice - August - 3,000 gallons

我不确定LINQ在这种情况下会有多大帮助

使用for循环可以相对简单地实现所需的转换:

' Sample data using anonymous types
Dim entries() =
    {New With {.Name = "Apple Cider", .Year = 2012, .Month = 9, .Quantity = 5000},
     New With {.Name = "Apple Cider", .Year = 2012, .Month = 12, .Quantity = 1000},
     New With {.Name = "Apple Juice", .Year = 2013, .Month = 9, .Quantity = 3000}}
' Dictionary keyed by date
Dim dict = entries.ToDictionary( _
    Function(entry) New DateTime(entry.Year, entry.Month, 1))
' Current is first entry
Dim currentEntry = entries(0)
Dim epoch = New DateTime(currentEntry.Year, currentEntry.Month, 1)
' Iterate over 24 months
For i = 0 To 23            
    Dim currentMonth = epoch.AddMonths(i)
    ' Update current based on current month
    If dict.ContainsKey(currentMonth) Then
        currentEntry = dict(currentMonth)
    End If
    ' Write data
    Console.WriteLine("{0} - {1} - {2} gallons",
                      currentEntry.Name,
                      MonthName(currentMonth.Month),
                      currentEntry.Quantity)
Next

要使用Linq执行此操作,请使用
Range()
(在可以在循环中执行某些操作的情况下,通常可以替换Range)

下面是一个使用Enumerable.Range和@PhillipTrelford示例数据的示例:

Sub Main
  Dim entries() =  {New With {.Name = "Apple Cider", .Year = 2012, .Month = 9, .Quantity = 5000}, _
                    New With {.Name = "Apple Cider", .Year = 2012, .Month = 12, .Quantity = 1000}, _
                    New With {.Name = "Apple Juice", .Year = 2013, .Month = 9, .Quantity = 3000}}

  Dim dict = entries.ToDictionary( Function(entry) New DateTime(entry.Year, entry.Month, 1))

  Dim startDate as DateTime = New DateTime(2012,9,1)

  Dim curDefault = New With {.Name = "", .Quantity = 0}
  Dim result = Enumerable.Range(0, 24).Select(Function(x)
    Dim thisdate = startDate.AddMonths(x)
    If dict.ContainsKey(thisdate) Then
      curDefault.Name = dict(thisdate).Name
      curDefault.Quantity = dict(thisdate).Quantity
    End If
    Return String.Format("{0} - {1} - {2} gallons",curDefault.Name,thisdate.ToString("MMMM"),curDefault.Quantity)

  End Function)

  result.Dump
End Sub
这段代码运行得很好,并使用linqpad提供了预期的结果(请参阅linqpad.com)


谢谢你修改我的问题,托马斯。这会让其他人更容易阅读我的帖子。谢谢你的回复。您的代码片段确实提供了我正在寻找的输出,但在您的回复和Phillip的回复中,我注意到您为条目添加了一个year属性,而我目前没有。然而,我看到了从一个日历年滚动到下一个日历年的重要性。(如果你在做+1,那么从12月到1月用1表示将不起作用)。在编字典时,我可以用当年而不是词条.year。我也可以使用当前年份,而不是2012年的开始日期。对吗?@GlenCiborowski-当然,只要确保代码的其余部分不期望某一年。
Sub Main
  Dim entries() =  {New With {.Name = "Apple Cider", .Year = 2012, .Month = 9, .Quantity = 5000}, _
                    New With {.Name = "Apple Cider", .Year = 2012, .Month = 12, .Quantity = 1000}, _
                    New With {.Name = "Apple Juice", .Year = 2013, .Month = 9, .Quantity = 3000}}

  Dim dict = entries.ToDictionary( Function(entry) New DateTime(entry.Year, entry.Month, 1))

  Dim startDate as DateTime = New DateTime(2012,9,1)

  Dim curDefault = New With {.Name = "", .Quantity = 0}
  Dim result = Enumerable.Range(0, 24).Select(Function(x)
    Dim thisdate = startDate.AddMonths(x)
    If dict.ContainsKey(thisdate) Then
      curDefault.Name = dict(thisdate).Name
      curDefault.Quantity = dict(thisdate).Quantity
    End If
    Return String.Format("{0} - {1} - {2} gallons",curDefault.Name,thisdate.ToString("MMMM"),curDefault.Quantity)

  End Function)

  result.Dump
End Sub
Apple Cider - September - 5000 gallons 
Apple Cider - October - 5000 gallons 
Apple Cider - November - 5000 gallons 
Apple Cider - December - 1000 gallons 
Apple Cider - January - 1000 gallons 
Apple Cider - February - 1000 gallons 
Apple Cider - March - 1000 gallons 
Apple Cider - April - 1000 gallons 
Apple Cider - May - 1000 gallons 
Apple Cider - June - 1000 gallons 
Apple Cider - July - 1000 gallons 
Apple Cider - August - 1000 gallons 
Apple Juice - September - 3000 gallons 
Apple Juice - October - 3000 gallons 
Apple Juice - November - 3000 gallons 
Apple Juice - December - 3000 gallons 
Apple Juice - January - 3000 gallons 
Apple Juice - February - 3000 gallons 
Apple Juice - March - 3000 gallons 
Apple Juice - April - 3000 gallons 
Apple Juice - May - 3000 gallons 
Apple Juice - June - 3000 gallons 
Apple Juice - July - 3000 gallons 
Apple Juice - August - 3000 gallons