如何将两个LINQ数据源合并到一个图表的单个表中?

时间:2015-01-16 23:25:10

标签: vb.net linq merge datasource

我正在尝试让两个人让我的SQL服务器为每个月的每一天计算两个不同的值,并将它们放在一个表中以便在图表中使用。

我尝试将它们合并到一个带有Union的DataSource中,但无法编译。以下代码为我提供了两个不同数据源所需的结果。

此代码为我提供了我需要的结果。

    Dim ChartDataOpen = (From a In db.Alarms _
             Where a.Generated >= FirstDayOfMonth(DateTimeNow) And a.Generated <= LastDayOfMonth(DateTimeNow) And a.IsClosed = False _
             Group a By CalendarDate = a.Generated.Value.Date Into g = Group _
             Select New With {CalendarDate, .AlarmCount = g.Count()})

    Dim ChartDataClosed = (From a In db.Alarms _
             Where a.Generated >= FirstDayOfMonth(DateTimeNow) And a.Generated <= LastDayOfMonth(DateTimeNow) And a.IsClosed = True _
             Group a By CalendarDate = a.Generated.Value.Date Into g = Group _
             Select New With {CalendarDate, .AlarmClosedCount = g.Count()})

此代码是我的问题,我找不到根据日历日期(2015年1月16日)合并两个数据源的巧妙方法,并将它们添加到单个数据源中以便在图表中使用。

    Dim ChartData = ChartDataOpen.Union(ChartDataClosed)

    RadHtmlChartAvg.DataSource = ChartData
    RadHtmlChartAvg.DataBind()

最终工作代码:

    Dim ChartData = (From a In db.Alarms _
     Where a.Generated >= FirstDayOfMonth(DateTimeNow) _
       And a.Generated <= LastDayOfMonth(DateTimeNow) _
     Group a By CalendarDate = a.Generated.Value.Date Into g = Group _
     Select New With {
          CalendarDate,
          .AlarmCount = g.Count(),
          .AlarmOpenCount = g.Count(Function(a) a.IsClosed = False),
          .AlarmClosedCount = g.Count(Function(a) a.IsClosed = True)
          })

2 个答案:

答案 0 :(得分:1)

创建具有相同属性的NAMED对象:

我可以用C#做​​到这一点。你需要转换为VB.NET

public class CalendarCount
{
     public dateTime CalendarDate {get; set;}
     public int Count {get; set;}
}

这部分我猜测vb语法:

Dim ChartDataOpen = (From a In db.Alarms _
         Where a.Generated >= FirstDayOfMonth(DateTimeNow) _
         And a.Generated <= LastDayOfMonth(DateTimeNow) And a.IsClosed = False _
         Group a By CalendarDate = a.Generated.Value.Date Into g = Group _
         Select New CalendarCount With {CalendarDate, .Count = g.Count()})

Dim ChartDataClosed = (From a In db.Alarms _
         Where a.Generated >= FirstDayOfMonth(DateTimeNow) _
         And a.Generated <= LastDayOfMonth(DateTimeNow) And a.IsClosed = True _
         Group a By CalendarDate = a.Generated.Value.Date Into g = Group _
         Select New CalendarCount With {CalendarDate, .Count = g.Count()})

答案 1 :(得分:1)

我认为您所追求的是一种结构,您可以在其中看到一行中的开放和封闭计数:

Dim ChartData = (From a In db.Alarms _
         Where a.Generated >= FirstDayOfMonth(DateTimeNow) _
           And a.Generated <= LastDayOfMonth(DateTimeNow) _
         Group a By CalendarDate = a.Generated.Value.Date Into g = Group _
         Select New With {
                             CalendarDate,
                            .AlarmCount = g.Count(Function(x) Not x.IsClosed)
                            .AlarmClosedCount = g.Count(Function(x) x.IsClosed)
                         })

您应该能够在每个日期有两个点或条形的图表中绘制它。