2014-02-23 47 views
0

我有一個存儲過程,它返回一些日期以及與表中特定行相關的Id。Linq使用Include與存儲過程

基本上,我得到一個賬戶組合中所有賬戶的所有計劃交易的清單。

存儲過程返回一個帶有Id(用於計劃事務)的行,以及一些我在proc中關注的日期。

如果我的查詢開始:

from p in Context.scheduled_transactions 

那麼這個計劃會奏效。但我不想獲得這樣的項目,因爲在proc中,我正在做很多工作來創建業務日期等等。所以,我沒有帶回EF模型 - 我的proc只是帶回了ID。我希望做這樣的事情:

var trans = (from p in Context.get_scheduled_payments_by_portfolio(portfolioId) 
              .Include("account") 
              .Include("cost_centre") 
              .Include("z_account_transaction_type") 
              .Include("z_payment_frequency_type") 
              .Include("transaction_sub_category") 
              .Include("transaction_sub_category.transaction_category") 
              .Include("third_party") 
         select p).ToList(); 

但是,該EF不能用「包括」,因爲它不知道我帶回來。雖然在過程中id被稱爲'scheduled_transaction_id' - EF不知道(可以理解)。

有沒有一種方法可以告訴EF,ID是針對scheduled_transaction_model - 然後使用'Include'?

也許我需要調用proc,它返回我的對象​​列表,其中包含scheduled_transaction_id和我在proc中計算的所有日期,然後以某種方式在另一個linq查詢中使用該列表<>可以加入其他表?編輯: 我可能會到一些東西!這不顯示語法錯誤。只需要創建一個新的類型......玩這個:

var trans = (from p in Context.get_scheduled_payments_by_portfolio(portfolioId) 
         join st in Context.scheduled_transaction 
              .Include("account") 
              .Include("cost_centre") 
              .Include("z_account_transaction_type") 
              .Include("z_payment_frequency_type") 
              .Include("transaction_sub_category") 
              .Include("transaction_sub_category.transaction_category") 
              .Include("third_party") 
              on p.scheduled_transaction_id equals st.id 
         select p).ToList(); 
+1

您的最後一句更適合解決您的問題。 –

+0

我在看,並玩這個,但...仍然不滿意'包括':從p在Context.get_scheduled_pa​​yments_by_portfolio(portfolioId) 加入st在Context.scheduled_transaction上p.scheduled_transaction_id等於st.id .Include(「account」) – Craig

+0

您可以創建一個返回'scheduled_transaction'對象的表值函數。你可以直接在這個函數的結果中包含'Include'。 –

回答

1
var ids = Context.get_scheduled_payments_by_portfolio(portfolioId).ToList(); 

var trans = (from p in Context.scheduled_transaction 
        .Include("account") 
        .Include("cost_centre") 
        .Include("z_account_transaction_type") 
        .Include("z_payment_frequency_type") 
        .Include("transaction_sub_category") 
        .Include("transaction_sub_category.transaction_category") 
        .Include("third_party") 
      where ids.Contains(p.id) 
      select p).ToList(); 

嘗試contains()方法將其轉換成SQL的IN(,,)語句。

0

的答案是,join的PROC到我用的桌子,然後我可以使用.Include()

var trans = (from p in Context.get_scheduled_payments_by_portfolio(portfolioId) 
         join st in Context.scheduled_transaction 
              .Include("account") 
              .Include("cost_centre") 
              .Include("z_account_transaction_type") 
              .Include("z_payment_frequency_type") 
              .Include("transaction_sub_category") 
              .Include("transaction_sub_category.transaction_category") 
              .Include("third_party") 
              on p.scheduled_transaction_id equals st.id 
         select new {st, p}).ToList(); 

然後用新的類型,我可以通過列表itterate,並建立我的對象。