LINQ to Entities:转换SQL Sub Select

时间:2012-02-13 15:59:33

标签: entity-framework linq-to-sql linq-to-entities

我弄明白了。 无需回答。 该系统说我必须等待8个小时才能回答我自己的问题。但目前答案如下:

以下是答案:

var startDate = DateTime.Today.AddDays(-30);    
var results = (from h in Histories
                join q in Quotes
                    on h.QuoteID equals q.QuoteID
                join a in Agencies
                    on q.AgencyID equals a.AgencyID             
                        where q.Status == "Inforce" &&   
                                q.LOB == "Vacant" &&        
                                q.EffectiveDate > startDate &&
                                h.Deleted == null &&
                                h.DeprecatedBy == null &&                                    
                                h.TransactionStatus == "Committed" &&                                        
                                a.DC_PLT_Roles.Any(r => r.Name == "Wholesaler")
                        group new {h} by new {h.PolicyNumber} into g        
                        select new {                                
                            MaxHistoryID = g.Max (x => x.h.HistoryID),
                            comment = (from h2 in Histories
                                    where h2.HistoryID == g.Max (x => x.h.HistoryID)
                                    select h2.Comment).FirstOrDefault() 
                            }).ToList();

关键代码是:

comment = (from h2 in Histories
                                    where h2.HistoryID == g.Max (x => x.h.HistoryID)
                                    select h2.Comment).FirstOrDefault() 

我们正在将SQL /存储过程转换为LINQ to Entities语句。我无法弄清楚子选择的正确语法。

目前我正在转换此SQL:

declare @startDate DateTime
set @startDate = DATEADD(DD, -30, GETDATE())

select * from history where historyid in(     
select  MAX(h.historyid) as HistoryId 
    from  History h (nolock) 
    inner join Quote q (nolock) on h.QuoteID = q.QuoteID 
    inner join Agency (nolock) a on q.AgencyID = a.AgencyID
    inner join DC_PLT_EntityRoles er (nolock) on a.AgencyID = er.EntityID
    inner join DC_PLT_Roles (nolock) r on er.RoleID = r.RoleID
    where
          q.Status = 'Inforce' 
          and q.LOB = 'Vacant'  
          and q.EffectiveDate > @startDate 
          and h.Deleted is null --
          and h.DeprecatedBy is null --
          and h.TransactionStatus = 'Committed'
          and r.Name = 'Wholesaler'
    group by h.PolicyNumber)

正如您所看到的,上面的代码由两个select语句组成。主要选择(从历史记录中选择*)和过滤器选择(选择MAX(h.historyid)...)

我让过滤器选择工作(见下文):

var startDate = DateTime.Today.AddDays(-30);    
var results = (from h in Histories
                join q in Quotes
                    on h.QuoteID equals q.QuoteID
                join a in Agencies
                    on q.AgencyID equals a.AgencyID             
                        where q.Status == "Inforce" &&   
                                q.LOB == "Vacant" &&        
                                q.EffectiveDate > startDate &&
                                h.Deleted == null &&
                                h.DeprecatedBy == null &&                                    
                                h.TransactionStatus == "Committed" &&                                        
                                a.DC_PLT_Roles.Any(r => r.Name == "Wholesaler")
                        group new {h} by new {h.PolicyNumber} into g        
                        select new {                                
                            MaxHistoryID = g.Max (x => x.h.HistoryID)                           
                            }).ToList();

但是我无法弄清楚设置主选择的正确语法。 (基本上使用过滤器选择中的HistoryID从History表中获取记录。)

任何帮助都将不胜感激。

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

我想通了,这是代码:

var startDate = DateTime.Today.AddDays(-30);    
var results = (from h in Histories
                        .Include("Quote")
                        .Include("Quote.Agency")                                    
                        where h.Quote.Status == "Inforce" &&     
                                h.Quote.LOB == "Vacant" &&      
                                h.Quote.EffectiveDate > startDate &&
                                h.Deleted == null &&
                                h.DeprecatedBy == null &&                                    
                                h.TransactionStatus == "Committed" &&                                        
                                h.Quote.Agency.DC_PLT_Roles.Any(r => r.Name == "Wholesaler")                            
                        group new {h} by new {h.PolicyNumber} into g        
                        select new {
                            XMLData = (from h2 in Histories
                                        where h2.HistoryID == g.Max (x => x.h.HistoryID)
                                        select h2.XMLData).FirstOrDefault() 
                            }).ToList();

关键逻辑是:

select new {
                            XMLData = (from h2 in Histories
                                        where h2.HistoryID == g.Max (x => x.h.HistoryID)
                                        select h2.XMLData).FirstOrDefault() 
                            }).ToList();

喜欢嵌套查询