0

我创建了一个 SQL Server 查询,该查询通过获取不同的案例 ID 列表并将它们连接到fnGetRelatedCases表函数来获得正确的结果。但是,我无法创建执行相同操作的实体框架查询。

SQL 服务器:

 select 
     cse.*
 from 
     (select distinct 
          CaseID
      from 
          ELFJuv.fnGetCalendar()
      where 
          Department = @Department 
          and HearingDateOnly = @FilterDate) as C
 outer apply 
     ELFJuv.fnGetRelatedCases(C.CaseID) as CSE
 where 
     CSE.CaseID is not null

下面的查询几乎可以工作,但它没有通过不同的案例 ID 列表加入。

实体框架:

IEnumerable<fnGetRelatedCases_Result> cases = 
(from calendar
 in _ctx.fnGetCalendar()
 where calendar.Department.Equals(department) &&
 calendar.HearingDateOnly == filterDate
 from fullCaseDetail in _ctx.fnGetRelatedCases(calendar.CaseID)
 select fullCaseDetail).ToList();

我需要将一个不同的案例 ID 列表传递到fnGetRelatedCases表函数中,但我无法正确加入,因为所有案例 ID 都被加入。

4

2 回答 2

0

我想出了如何解决这个问题。

(from calendar in _ctx.fnGetCalendar()
.Where(d => d.HearingDateOnly == filterDate && d.Department == department)
.Select(x => new { x.CaseID })
.Distinct()
from fullCaseDetail in _ctx.fnGetRelatedCases(calendar.CaseID)
select fullCaseDetail).ToList();
于 2016-01-19T19:38:05.340 回答
0

干得好

var cases = 
   (from caseID in (from calendar in _ctx.fnGetCalendar()
               where calendar.Department.Equals(department)
                   && calendar.HearingDateOnly == filterDate
               select calendar.CaseID).Distinct()
    from fullCaseDetail in _ctx.fnGetRelatedCases(caseID)
    select fullCaseDetail).ToList();

这相当于

// Subquery
var subquery =
    (from caseID in (from calendar in _ctx.fnGetCalendar()
     where calendar.Department.Equals(department)
         && calendar.HearingDateOnly == filterDate
         select calendar.CaseID).Distinct();
// Query
var query = 
    from caseID in subquery
    from fullCaseDetail in _ctx.fnGetRelatedCases(caseID)
    select fullCaseDetail;
// Result
var cases = query.ToList();
于 2016-01-19T19:45:06.090 回答