Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.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
Asp.net mvc 如何使用默认值填充列表中不包含';数据库中不存在吗?_Asp.net Mvc_Vb.net - Fatal编程技术网

Asp.net mvc 如何使用默认值填充列表中不包含';数据库中不存在吗?

Asp.net mvc 如何使用默认值填充列表中不包含';数据库中不存在吗?,asp.net-mvc,vb.net,Asp.net Mvc,Vb.net,我想在数据库中记录过去一年的数据,按月汇总记录,然后用这些信息填充一个折线图。然而,当一个月没有记录时,我似乎不知道如何把它放在列表中的适当位置。例如,如果9月或10月不存在任何数据,我的折线图将跳过这些月份。我试着在之后检查并添加月份,但我无法按顺序获得它们。有什么帮助吗 Dim PointTotals = db.MemberRewards _ .Where(Function(r) sitewideFilterSelectedMemberIds.Contains(r.m

我想在数据库中记录过去一年的数据,按月汇总记录,然后用这些信息填充一个折线图。然而,当一个月没有记录时,我似乎不知道如何把它放在列表中的适当位置。例如,如果9月或10月不存在任何数据,我的折线图将跳过这些月份。我试着在之后检查并添加月份,但我无法按顺序获得它们。有什么帮助吗

Dim PointTotals = db.MemberRewards _
            .Where(Function(r) sitewideFilterSelectedMemberIds.Contains(r.memberId) And r.supplier.name <> "AudStandard" And r.transactionDate >= startDate And r.transactionDate <= EndDate) _
            .GroupBy(Function(r) New With {r.transactionDate.Value.Month, r.transactionDate.Value.Year}) _
            .Select(Function(gr) New With {.month = gr.Key.Month, .year = gr.Key.Year, .totalPoints = gr.Sum(Function(r) r.points)}) _
            .OrderBy(Function(gr) gr.year).ThenBy(Function(gr) gr.month)

Dim firstPeriodDate As Date
Dim currentDate As Date = DateAdd(DateInterval.Month, -1, startDate)

If PointTotals.Count > 0 Then
    Dim firstPeriod = PointTotals.First
    firstPeriodDate = CDate(firstPeriod.month & "/1/" & firstPeriod.year)
Else
    firstPeriodDate = EndDate
End If

Dim months As New List(Of String)
Dim Points As New List(Of Integer)
Do While currentDate < firstPeriodDate
    months.Add(currentDate.ToString("MMM"))
    Points.Add(0)
    currentDate = DateAdd(DateInterval.Month, 1, currentDate)
Loop

For Each period In PointTotals
    months.Add(CDate(period.month & "/1/" & period.year).ToString("MMM"))
    Points.Add(period.totalPoints)
Next

ViewBag.Months = """" & String.Join(""",""", months.ToArray) & """"
ViewBag.Points = String.Join(",", Points.ToArray)
Dim PointTotals=db.MemberRewards_
其中(函数(r)SiteWideFilterSelectedMemberId.Contains(r.memberId)和r.supplier.name“AudStandard”和r.transactionDate>=开始日期和r.transactionDate 0然后
Dim firstPeriod=点总计。第一个
firstPeriodDate=CDate(firstPeriod.month&“/1/”&firstPeriod.year)
其他的
firstPeriodDate=EndDate
如果结束
变暗月份作为新列表(字符串)
将点调整为新列表(整数)
当currentDate
在您的代码中,添加缺少的月份听起来像是使用了.add方法。您需要使用months.Insert(position,CDate(..)和Points.Insert(position,0),其中position是按正确顺序插入月份的正确索引


我可以给你确切的命令,但你没有包括你在问题中引用的清理代码。

有很多方法可以解决这个问题,我将提供一个DB方法

由于您连接到数据库以提取数据,因此还可以在数据库端进行汇总/分组

您可以通过3个步骤完成此操作: 1) 把你所有的月份都列出来,并把它们存储在临时表中 2) 按月创建汇总,并将其存储在另一个临时表中 3) 在第2步(使用月作为加入标准)中留下加入步骤1,按照您关心的顺序进行排序,现在您拥有了所有的月份


有很多方法可以在SQL端实现这一点,上面的方法是我认为很容易遵循的方法。

我认为这是一个比尝试在循环后清理列表更优雅的解决方案。对代码的唯一更改就在For-Each-Period循环之前和中

Dim PointTotals = db.MemberRewards _
        .Where(Function(r) sitewideFilterSelectedMemberIds.Contains(r.memberId) And r.supplier.name <> "AudStandard" And r.transactionDate >= startDate And r.transactionDate <= EndDate) _
        .GroupBy(Function(r) New With {r.transactionDate.Value.Month, r.transactionDate.Value.Year}) _
        .Select(Function(gr) New With {.month = gr.Key.Month, .year = gr.Key.Year, .totalPoints = gr.Sum(Function(r) r.points)}) _
        .OrderBy(Function(gr) gr.year).ThenBy(Function(gr) gr.month)

        Dim firstPeriodDate As Date
        Dim currentDate As Date = DateAdd(DateInterval.Month, -1, startDate)

        If PointTotals.Count > 0 Then
            Dim firstPeriod = PointTotals.First
            firstPeriodDate = CDate(firstPeriod.month & "/1/" & firstPeriod.year)
        Else
            firstPeriodDate = EndDate
        End If

        Dim months As New List(Of String)
        Dim Points As New List(Of Integer)
        Do While currentDate < firstPeriodDate
            months.Add(currentDate.ToString("MMM"))
            Points.Add(0)
            currentDate = DateAdd(DateInterval.Month, 1, currentDate)
        Loop

        Dim thisPeriodDate As Date
        Dim previousPeriodDate As Date = currentDate
        For Each period In PointTotals
            thisPeriodDate = CDate(period.month & "/1/" & period.year)
            Do While DateDiff(DateInterval.Month, previousPeriodDate, thisPeriodDate) > 1
                months.Add(previousPeriodDate.ToString("MMM"))
                Points.Add(0)
                previousPeriodDate = DateAdd(DateInterval.Month, 1, previousPeriodDate)
            Loop
            months.Add(thisPeriodDate)
            Points.Add(period.totalPoints)
            previousPeriodDate = DateAdd(DateInterval.Month, 1, previousPeriodDate)
        Next

        ViewBag.Months = """" & String.Join(""",""", months.ToArray) & """"
        ViewBag.Points = String.Join(",", Points.ToArray)
Dim PointTotals=db.MemberRewards_
其中(函数(r)SiteWideFilterSelectedMemberId.Contains(r.memberId)和r.supplier.name“AudStandard”和r.transactionDate>=开始日期和r.transactionDate 0然后
Dim firstPeriod=点总计。第一个
firstPeriodDate=CDate(firstPeriod.month&“/1/”&firstPeriod.year)
其他的
firstPeriodDate=EndDate
如果结束
变暗月份作为新列表(字符串)
将点调整为新列表(整数)
当currentDate1
月数。添加(上一个周期日期。ToString(“MMM”))
点数。添加(0)
previousPeriodDate=DateAdd(DateInterval.Month,1,previousPeriodDate)
环
月份。添加(此周期日期)
点数。添加(句点。总点数)
previousPeriodDate=DateAdd(DateInterval.Month,1,previousPeriodDate)
下一个
ViewBag.Months=“”&String.Join(“,”,Months.ToArray)和“
ViewBag.Points=String.Join(“,”,Points.ToArray)

好的,这将索引它,这是正确的一步。但是我有一个站点范围的过滤器,用户可以选择更改日历的结束日期。有时,它可以从2月运行到1月,其他时间可以从9月运行到8月,等等。因此,列表中的索引并不总是与月份相关例如“月。插入(0,“一月”)”不总是有效的。我想我找到了一个更优雅的解决方案。我将添加它作为一个新的答案,以供将来研究类似问题的人参考。让我知道它是否有用。太棒了!超级接近了。这在正确的位置添加了一个月,但不是全部。例如,不是从9月跳到12月十月,现在显示为零,然后跳过十一月。另外,我需要在Do-While循环结束后在行中的“thisPeriodDate”中添加“.ToString”(“MMM”)。这只是一个小补丁,供其他阅读此内容的人使用。通过将“Do-While-DateDiff(DateInterval.Month,previousPeriodDate,thisPeriodDate)>1”更改为“Do-While-DateDiff”(DateInterval.Month,previousPeriodDate,thisPeriodDate)>0“,它似乎工作得很好。谢谢!!我知道我做了什么。如果您将Dim previousPeriodDate更改为Date=currentDate,将Dim previousPeriodDate更改为Date=Date添加(DateInterval.Month,-1,currentDate)它应该适用于所有情况。我开始两个日期相等。我不认为您所做的更改会导致问题,但更改起始值更具可读性。很高兴我能提供帮助。:)不知道如何在Linq SQL中执行此操作,但在Select中它将是
IsNull(总和(点),0)