繁体   English   中英

Linq使用包含在存储过程中

[英]Linq using Include with Stored Procedure

我有一个存储过程,它返回一些日期,以及与表中特定行相关的ID。

基本上,我会获得一个帐户投资组合中所有帐户的所有预定交易的列表。

存储过程返回一个带有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无法使用“包含”,因为它不知道我要带回什么。 尽管在proc中将该ID称为“ scheduled_transaction_id”,但EF不知道(可以理解)。

有没有一种方法可以告诉EF ID是Scheduled_transaction_model的-然后使用“包含”?

也许我只需要调用proc,它会向我返回我的对象​​的列表,该对象具有schedule_transaction_id,以及在proc中计算的所有日期,然后以某种方式在另一个可以连接其他桌子?

编辑:我可能正在做某事! 这没有显示语法错误。 只需创建一个新的Type ...玩这个:

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();
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(,,)语句。

答案是,将proc join到我正在使用的表中,然后可以使用.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();

然后使用新类型,我可以遍历列表并构建对象。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM