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

时间:2014-03-30 22:51:36

标签: asp.net vb.net linq range date-range

我需要帮助在一个日历年中应用设置。我的用户根据日历中的季节将数字目标输入数据库。想想,苹果酒将在秋季(9月至11月)销售较高,而在今年剩余时间(12月至8月)销售较少。然而,苹果汁全年均匀销售。

为了简化用户的数据输入,他们输入目标销售数量以及每个产品开始的月份。

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

Apple Cider - September - 5,000 gallons
Apple Cider - December - 1,000 gallons
Apple Juice - September - 3,000 gallons

我还将财务日历放在数据库表中,因此我知道会计财政年度,月份,月份的周数等等。

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

我在使用LINQ加入我的会计日历以创建数据的爆炸式视图时遇到问题?如何从一年中所有月份的列表开始,对于每个产品,将目标放在日历中,然后填写空白。例如,使用上面示例的Apple Cider

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

2 个答案:

答案 0 :(得分:0)

我不确定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

答案 1 :(得分:0)

要使用Linq使用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)

给出预期结果
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