2013-10-02 132 views
0

我有一個複雜的EF查詢掙扎複雜的實體框架查詢

查詢傳遞一個整數,它定義的天數回頭看A股股票收盤價以確定是否從最近每天較低那麼先前的(即看看回顧期間的價格是否較低)我正在努力如何納入價格測試。我可以檢索所需的天數,但不確定是否可以在一個查詢中完成整個過程,或者是否應該將其分解爲另一個查詢以獲得結果。任何幫助讚賞。 (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 

回答

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,但我離開了對於每個循環,因爲它使整個事情變得更容易跟隨比一個巨大的LINQ聲明。

+0

感謝您的回覆,enroute首頁,將在那裏測試 – dinotom