2014-02-23 46 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,并建立我的对象。