Linq 在实体框架中使用子查询

Linq 在实体框架中使用子查询,linq,entity-framework,linq-to-sql,Linq,Entity Framework,Linq To Sql,我试图编写实体框架linq查询来生成以下SQL。但我不知道如何使用实体框架的子查询 我要生成的Sql是: Declare @StartDate Datetime2; Set @Startdate = '2014-Feb-16 09:52' Declare @EndDate Datetime2; Set @Enddate = '2014-Feb-18 09:52' SELECT [D].[RefId] ,[D].[StatusId] ,[D].

我试图编写实体框架linq查询来生成以下SQL。但我不知道如何使用实体框架的子查询

我要生成的Sql是:

Declare @StartDate Datetime2; Set @Startdate = '2014-Feb-16 09:52'
Declare @EndDate Datetime2; Set @Enddate = '2014-Feb-18 09:52'

 SELECT
         [D].[RefId]
        ,[D].[StatusId]
        ,[D].[StatusDate]
        ,[D].[Reference]
        ,[RSC].[Event] 
        ,[RSC].[Information] 
        ,[RSC].[CreatedDate] 
  FROM (
    SELECT
           [R].[RefId]
          ,[R].[StatusId]
          ,[R].[StatusDate]
          ,[I].[Reference]          
          ,(SELECT TOP 1
                   [RSC].[ChangeId]
            FROM
                   [dbo].[StateChangeTable] AS [RSC] (nolock)
            WHERE
                   [RSC].[RefId] = [R].[RefId]
            ORDER BY
                   [RSC].[ChangeId] DESC) AS [LastChangeId]
    FROM
           [dbo].[Table1] AS [R] (nolock)
    INNER JOIN
           [dbo].[Table2] AS [I] (nolock)
    ON
           [R].[RefId] = [I].[RefId]

    WHERE
           [R].[StatusId] IN (4, 6)
    AND    [R].[StatusDate] between @StartDate and @EndDate
    ) AS [D]
  INNER JOIN
         [dbo].[StateChangeTable] AS [RSC] (nolock)
  ON
         [D].[LastChangeId] = [RSC].[ChangeId
]

到目前为止我写的代码是:

return this.DbContext.Table1
            .Join(this.DbContext.Table2, rc => rc.RefId, ri => ri.RefId, (rc, ri) => new { rc, ri })                     
            .Join(this.DbContext.StateChangeTable, request => request.ri.RefId,  rsc => rsc.RefId, (request, rsc) => new {request, rsc})
           .Where(r => (r.rsc.ChangeId == ((from rsc in this.DbContext.StateChangeTable                                                               
                                                orderby rsc.ChangeId descending
                                                select rsc.ChangeId).FirstOrDefault())) &&
                        (r.request.rc.StatusId == 4 || r.request.rc.StatusId == 6) &&
                        (r.request.rc.StatusDate >= startDateTime && r.request.rc.StatusDate <= endDateTime))              
            .Select(requestDetails => new StatusDetail
                {
                    RefId = requestDetails.request.rc.RefId,
                    StatusDate = requestDetails.request.rc.StatusDate,
                    StatusId = requestDetails.request.rc.StatusId,
                    Reference = requestDetails.request.ri.DistributionReference.Value,
                    Event = requestDetails.rsc.Event,
                    CreatedDate = requestDetails.rsc.CreatedDate,
                    Information = requestDetails.rsc.Information
                }).ToList();
返回this.DbContext.Table1
.Join(this.DbContext.Table2,rc=>rc.RefId,ri=>ri.RefId,(rc,ri)=>new{rc,ri})
.Join(this.DbContext.StateChangeTable,request=>request.ri.RefId,rsc=>rsc.RefId,(request,rsc)=>new{request,rsc})
。其中(r=>(r.rsc.ChangeId==((来自this.DbContext.StateChangeTable中的rsc
orderby rsc.ChangeId降序
选择rsc.ChangeId).FirstOrDefault())&&
(r.request.rc.StatusId==4 | | r.request.rc.StatusId==6)&&
(r.request.rc.StatusDate>=startDateTime和&r.request.rc.StatusDate新状态详细信息
{
RefId=requestDetails.request.rc.RefId,
StatusDate=requestDetails.request.rc.StatusDate,
StatusId=requestDetails.request.rc.StatusId,
Reference=requestDetails.request.ri.DistributionReference.Value,
Event=requestDetails.rsc.Event,
CreatedDate=requestDetails.rsc.CreatedDate,
Information=requestDetails.rsc.Information
}).ToList();
能告诉我我做错了什么吗

非常感谢

不要使用
.Join()
您必须使用实体上的导航属性。

以下是完整的查询

var query = (from D in
                 ((from tab1 in DbContext.Table1
                   join tab2 in DbContext.Table2 on tab1.RefId equals tab2.RefId
                   where (tab1.StatusId == 4 || tab1.StatusId == 6)
                          && (tab1.StatusDate >= startDate && tab1.StatusDate <= endDate)
                   select new
                   {
                       RefId = tab1.RefId,
                       StatusId = tab1.StatusId,
                       StatusDate = tab1.StatusDate,
                       Reference = tab2.Reference,
                       LastChangeId = (from RSC in DbContext.StateChangeTable
                                       where RSC.RefId == tab1.RefId
                                       orderby RSC.ChangeId descending
                                       select RSC.ChangeId).FirstOrDefault()
                   }))
             join RSC in DbContext.StateChangeTable on D.LastChangeId equals RSC.ChangeId
             select new StatusDetail
             {
                 RefId = D.RefId,
                 StatusId = D.StatusId,
                 StatusDate = D.StatusDate,
                 Reference = D.Reference,
                 Event = RSC.Event,
                 Information = RSC.Information,
                 CreatedDate = RSC.CreatedDate
             }).ToList();
var query=(从中的D开始)
((来自DbContext.Table1中的tab1)
在tab1.RefId上的DbContext.Table2中连接tab2等于tab2.RefId
其中(tab1.StatusId==4 | | tab1.StatusId==6)

&&(tab1.StatusDate>=startDate&&tab1.StatusDate那么出了什么问题?我不知道如何在entityframework中编写子查询(从[dbo].[StateChangeTable]中选择前1个[RSC].[ChangeId]作为[RSC](nolock),其中[RSC].[RefId]=[R].[RefId]ORDER BY[RSC].[ChangeId]DESC作为[LastChangeId])目前我在linq中写过,但它生成的是左外连接而不是子查询。不确定这是否正确。不,您不必总是使用导航属性。虽然它们通常可以简化查询,但有时指定显式连接而不是使用导航属性的隐式连接可以提供更好、更具针对性和效率的查询结果令人震惊。