【问题标题】:cant return a sorted iqueryable of datetime无法返回日期时间的排序 iqueryable
【发布时间】:2014-04-06 21:57:40
【问题描述】:

当我尝试返回此日期列表时,总是得到错误的结果

更新

  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

不发生排序,返回整个日期列表

我以前按日期排序,既使用函数语法,也将 OrderBy 放在 linq 语句中的 Select 之前。我只是想在不同的日期列表中返回最近的三个日期。

但是,如果我将 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) 实际上只返回三个日期。问题是它们总是最老的,而我需要最新的。

【问题讨论】:

  • 对于初学者来说,它应该只返回三个日期(在例行的即时测试中)并返回所有 35 个日期。我也不确定如何使用这样的函数语法对升序进行排序,如果这比在 LINQ 语句本身中使用 OrderBy 更有效的话。

标签: vb.net linq entity-framework


【解决方案1】:

哇,很蹩脚,但它的工作原理。任何能改进这种方法的人都会是我的英雄,因为我确信有比这个答案描述的方法更好的方法来完成这个。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-12-14
    • 1970-01-01
    • 2018-05-16
    • 1970-01-01
    • 2011-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多