无法返回一个排序的iqueryable日期时间

时间:2014-04-06 21:57:40

标签: vb.net linq entity-framework

当我尝试返回此日期列表

时,继续得到错误的结果

更新

  Private Shared Function GetDistinctDates(ByRef ctx As ToolkitEntities,
                                             ByVal symbol As String,
                                             ByVal interval As Integer,
                                             Optional isRangeProj As Boolean = False) As  List(Of Date)
        Dim numDates As Integer = 0
        If isRangeProj = True Then
            numDates = 3
        Else
            numDates = TakeValue(interval)
        End If

        Dim dates = (From data In ctx.tsintracharts
                     Where data.Symbol = symbol
                     Select data.Date).Distinct().ToList()

        'Dim datelist As IQueryable(Of Date) = dates.OrderByDescending(Function(o) o).Take(numDates)
        Return dates
    End Function

没有排序,返回整个日期列表

我之前按日期排序,都使用函数语法,并在Linq语句中的Select之前放置OrderBy。我只是想在不同的日期列表中返回最新的三个日期。

但是,如果我将Order By移动到LINQ语句中,则不会发生排序

  Dim dates = (From data In ctx.tsintracharts
                    Where data.Symbol = symbol
                    Order By data.Date Descending
                    Select data.Date
                    ).Distinct()

并返回dates.Take(numDates)确实只返回三个日期。问题是它们总是最老的,我需要最新的。

1 个答案:

答案 0 :(得分:0)

哇,非常蹩脚,但它确实有效。任何能够更好地使用这种方法的人都会成为我的英雄,因为我确信有更好的方法来完成这个,而不是这个答案描述的方法。

Private Shared Function GetDistinctDates(ByRef ctx As ToolkitEntities,
                                             ByVal symbol As String,
                                             ByVal interval As Integer,
                                             Optional isRangeProj As Boolean = False) As List(Of Date)
        Dim numDates As Integer = 0
        If isRangeProj = True Then
            numDates = 3
        Else
            numDates = TakeValue(interval)
        End If

        Dim dates As List(Of Date) = (From d In ctx.tsintracharts
                                      Where d.Symbol = symbol
                                      Select d.Date).Distinct().ToList()

        Dim datelist As New List(Of Date)
        For x As Integer = 1 To dates.Count
            If x > (dates.Count - numDates) Then
                datelist.Add(dates(x - 1))
            End If
        Next
        Return datelist
    End Function