SQL to LINQ with outer apply

时间:2017-09-09 18:01:43

标签: c# linq

由于外部申请

,我无法将sql转换为linq
select * from dbo.Table1 l
inner join dbo.Table2 d on d.LoanId = l.Id
inner join dbo.Cash cOriginal on cOriginal.DealId = d.Id and cOriginal.IsOriginal = 1
outer apply (select top 1 * from dbo.Cash cActive
             where cActive.DealId = d.ID and cActive.IsOriginal = 0
             order by cActive.CreatedOn desc) cActiveRes

我开始这样的事情:

var q = from x in _repo.Queryable<Table1>()
 join Table2 in _repo.Queryable<Table2>() on x.Id equals Table2.LoanId
 join cOriginal in _repo.Queryable<Cash>() on Table2.Id equals 
                                                           cOriginal.DealId
 // now Outer Apply should come?
 // fActive in _repo.Queryable<Cash>() 

1 个答案:

答案 0 :(得分:0)

直接运行SQL查询的最简单方法是让EF实现结果。它可能看起来像这样(取决于您使用的EF版本):

var result = ((IObjectContextAdapter)_repo).ObjectContext.ExecuteStoreQuery<Table1>(
               @"SELECT your query here");
相关问题