LINQ to Entities:转换 SQL 子选择

发布于 2025-01-04 17:36:40 字数 3891 浏览 5 评论 0原文

我明白了这一点。 无需回答。 系统说我必须等待 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 从历史表中获取记录。)

任何帮助将不胜感激。

感谢您的帮助。

I got this figured out.
No need to answer.
The system says I have to wait 8 hours before answering my own questions. But for now the answer is below:

Here is the answer:

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();

The key code was:

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

We are in the process of converting SQL / Stored Procedures to LINQ to Entities statements. And I can’t figure out the proper syntax for a sub select.

Currently I am converting this 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)

As you can see the code above is made up of two select statements. The main select (select * from history).. And a filter select (select MAX(h.historyid)…)

I got the filter select working (See below):

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();

However I can’t figure out the proper syntax to set up the main select. (Basically getting the records from the History table using the HistoryID from the filter select.)

Any help would be appreciated.

Thanks for your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦年海沫深 2025-01-11 17:36:40

我想通了,这是代码:

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();

必须喜欢嵌套查询

I figured it out, here is the code:

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();

The key logic is:

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

Gotta love the Nested Query

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文