【问题标题】:complex entity framework query复杂实体框架查询
【发布时间】:2023-03-20 19:00:02
【问题描述】:

我在处理一个复杂的 EF 查询

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

【问题讨论】:

    标签: vb.net entity-framework


    【解决方案1】:

    你真的应该有 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 语句更容易理解。

    【讨论】:

    • 感谢回复,回家途中,到时去测试
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-16
    • 2011-12-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多