We are in the process of converting SQL / Stored Procedures to LINQ to Entities statements.
Currently I am converting this SQL:
declare @startDate DateTime
set @startDate = DATEADD(DD, -30, GETDATE())
select h.*
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'
This is the LINQ to Entities which I wrote up:
var startDate = DateTime.Today.AddDays(-30);
var results = (from h in Histories
.Include("Quote")
.Include("Quote.Agency")
.Include("Quote.Agency.DC_PLT_Roles")
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.All(r => r.Name == "Wholesaler")
select h).ToList();
The problem is the SQL returns 77 rows and my LINQ returns none.
Anyone have any ideas on how I can convert this correctly? Or what I am missing.
Thanks for your help.
R
h.Quote.Agency.DC_PLT_Roles.All(r => r.Name == "Wholesaler")I think this line is your problem. I’m pretty sure you want to use
Anyhere notAll. In this instanceAllmeans all DC_PLT_Roles for the Agency must haveName == "Wholesaler".Anymeans there must be one DC_PLT_Role for the agency withName == "Wholesaler".