0

我正在为复杂的 EF 查询而苦苦挣扎

查询传递了一个整数,它定义了回顾股票收盘价的天数,以确定最近的每一天是否低于前一天(即查看回顾期间的每一天价格是否较低) I我正在为如何结合价格测试而苦苦挣扎。我可以检索必要的天数,但不确定我是否可以在一个查询中完成整个过程,或者是否应该将其分解为另一个查询以获得结果。任何帮助表示赞赏。(AddBusinessDays 是我自己的函数,只使用工作日)

 Public Shared Function GetDailyEquityDownMoves(ByVal numdaysback As Int32) As List(Of String)
    Dim qualifiedDownStocks As New List(Of String)
    Using ctx As New QE2DatabaseEntities()
        Dim symblist As List(Of Int64) = (From sym In ctx.symbols Select sym.idsymbols).ToList()
        For Each symId As Int64 In symblist
            Dim query = (From q In ctx.pricedatas
                             Where q.symid = symId
                             Join d In ctx.symbols On d.idsymbols Equals symId
                             Where q.pricedate < DateTime.Now AndAlso q.pricedate > AddBusinessDays(DateTime.Now.AddDays(numdaysback * -1))
                             Order By q.pricedate Ascending
                             Select q.closeprice).ToList()
            'Now test to see if each closeprice is less than the prior starting with most recent date
            'if it qualifies add it to the list
            'qualifiedDownStocks.Add(result)

        Next



    End Using
End Function
4

1 回答 1

0

你真的应该有 Option Strict On。既然不是,我不得不猜测数据类型,因为您没有强烈键入查询。另外,您并没有准确说出要添加到列表中的内容,所以我猜symId。如果我的猜测是错误的,请更改该部分。无论如何,看看这样的事情是否可行:

Public Shared Function GetDailyEquityDownMoves(ByVal numdaysback As Int32) As List(Of String)
    Dim qualifiedDownStocks As New List(Of String)
    Using ctx As New QE2DatabaseEntities()
        Dim symblist As List(Of Int64) = ctx.symbols.ToList        

        For Each symId As Int64 In symblist
            Dim previousPrice As Integer = 0
            Dim query As IEnumerable (Of pricedata) = (From q In ctx.pricedatas
                                                       Where q.symid = symId
                                                       Join d In ctx.symbols On d.idsymbols Equals symId
                                                       Where q.pricedate < DateTime.Now  AndAlso q.pricedate > AddBusinessDays(DateTime.Now.AddDays(numdaysback * -1))
                                                       Order By q.pricedate Ascending
                                                       Select q)

            'Now test to see if each closeprice is less than the prior starting with most recent date
            For Each closing As pricedata In query
                Dim closingPrice As Decimal = closing.closeprice
                'if it qualifies add it to the list
                If closingPrice < previousPrice Then
                    qualifiedDownStocks.Add(closing.symId & vbTab & closing.pricedate)
                End If
                previousPrice = closingPrice
            Next

        Next       

    End Using
End Function

您可能可以使用 LINQ 完成所有操作,但我留下了 For Each 循环,因为它使整个操作比一个巨大的 LINQ 语句更容易理解。

于 2013-10-02T20:54:32.533 回答