0

我需要获取在一个时间跨度内创建的所有 CustomerOrders,按天对它们进行分组(无论那天是什么时间),然后还包括每天的 CustomerOrders 计数。我的 ChartDateCount 类有两个属性 - 日期和计数。

因此,这里有一些示例 SQL 数据:

OrderID     Created
1           1/1/2012 04:30:12  
2           1/1/2012 05:15:29  
3           1/1/2012 07:09:45  
4           1/3/2012 01:12:21  
5           1/4/2012 06:33:58  
6           1/4/2012 08:30:26  
7           1/5/2012 10:17:41  
8           1/5/2012 11:30:43  
9           1/6/2012 01:11:11  

我的输出应该是:

Date         Count
1/1/2012     3
1/3/2012     1
1/4/2012     2
1/5/2012     2
1/6/2012     1

这就是我到目前为止所拥有的。

Dim chartData as List(Of ChartDateCount) = _
         ( _
            From co In dc.CustomerOrders _
            Where co.Created >= fromDate _
            And co.Created <= toDate _
            Select New ChartDateCount With {.Date = ???, .Count = ???} _
         ).ToList()

我有点接近这个,但我无法让 Date 填充:

From co In CustomerOrders _
Where co.Created >= "1/1/2012" And co.Created <= "1/7/2012" _
Group co By co.Created.Value.Date Into g = Group _
        Select New ChartDateCount With {.Date = ????, .Count = g.Count()}

更新

这正是我在理论上想要做的(见下文),但我得到一个错误这个错误:The query operator 'ElementAtOrDefault' is not supported.

From co In CustomerOrders _
Where co.Created >= "1/1/2012" And co.Created <= "1/7/2012" _
Group co By co.Created.Value.Date Into g = Group _
        Select New ChartDateCount With {.Date = g(0).Created.Value.Date, .Count = g.Count()}
4

2 回答 2

1

我想你正在寻找g.Key

 Select New ChartDateCount With {.Date = g.Key, .Count = g.Count()}

Key 属性包含分组依据的值。

http://msdn.microsoft.com/en-us/library/bb343251.aspx

于 2012-09-18T14:52:31.943 回答
0

我想出了一个方法,但如果有人有更有效的方法来解决这个问题,我肯定会给予你信任......

Dim chartData as List(Of ChartDateCount) = _
    (
        From co In dc.CustomerOrders _
                Where co.Created > fromDate _
                   And co.Created < toDate _
            Group By co.Created.Value.Date Into g = Group _
                   Select New ChartNameValue With _
                   {
                      .Date = (From co2 As CustomerOrder In g).Take(1).Single().Created.Value.Date, _
                      .Count = g.Count()
                   }
     ).ToList()
于 2012-09-18T17:05:13.770 回答