I am having a lot of trouble coming up with the Linq equivalent of this legacy stored procedure. The biggest hurdle is it doesn't seem to want to let me add a second 'clause' on the join with that tblAddress
. I am getting a Cannot resolve method...
error.tblBusiness.tblAddress
is seen as an EntitySet<tblAddress>
See bottom for current effort.
Can anyone point out what I am doing wrong? Below is, first, the SPROC
I need to convert and, second, my LINQ
attempt so far; which is FULL OF FAIL!
Thanks
SELECT dbo.tblPersonInsuranceCoverage.PersonInsuranceCoverageID,
dbo.tblPersonInsuranceCoverage.EffectiveDate,
dbo.tblPersonInsuranceCoverage.ExpirationDate,
dbo.tblPersonInsuranceCoverage.Priority,
dbo.tblAdminInsuranceCompanyType.TypeName AS CoverageCategory,
dbo.tblBusiness.BusinessName,
dbo.tblAdminInsuranceType.TypeName AS TypeName,
CASE WHEN dbo.tblAddress.AddressLine1 IS NULL THEN '' ELSE dbo.tblAddress.AddressLine1 END
+ ' ' +
CASE WHEN dbo.tblAddress.CityName IS NULL THEN '' ELSE '<BR>' + dbo.tblAddress.CityName END
+ ' ' +
CASE WHEN dbo.tblAddress.StateID IS NULL THEN ''
WHEN dbo.tblAddress.StateID = 'ns' THEN ''
ELSE dbo.tblAddress.StateID END AS Address
FROM
dbo.tblPersonInsuranceCoverage
LEFT OUTER JOIN dbo.tblInsuranceCompany
ON dbo.tblPersonInsuranceCoverage.InsuranceCompanyID = dbo.tblInsuranceCompany.InsuranceCompanyID
LEFT OUTER JOIN dbo.tblBusiness
ON dbo.tblBusiness.BusinessID = dbo.tblInsuranceCompany.BusinessID
LEFT OUTER JOIN dbo.tblAddress
ON dbo.tblAddress.BusinessID = dbo.tblBusiness.BusinessID and tblAddress.AddressTypeID = 'b'
LEFT OUTER JOIN dbo.tblAdminInsuranceCompanyType
ON dbo.tblPersonInsuranceCoverage.InsuranceCompanyTypeID = dbo.tblAdminInsuranceCompanyType.InsuranceCompanyTypeID
LEFT OUTER JOIN dbo.tblAdminInsuranceType
ON dbo.tblPersonInsuranceCoverage.InsuranceTypeID = dbo.tblAdminInsuranceType.InsuranceTypeID
WHERE tblPersonInsuranceCoverage.PersonID = @PersonID
var coverage =
from insuranceCoverage in context.tblPersonInsuranceCoverages
where insuranceCoverage.PersonID == personID
select
new
{
insuranceCoverage.PersonInsuranceCoverageID,
insuranceCoverage.EffectiveDate,
insuranceCoverage.ExpirationDate,
insuranceCoverage.Priority,
CoverageCategory = insuranceCoverage.tblInsuranceCompany.tblAdminInsuranceCompanyType.TypeName,
insuranceCoverage.tblInsuranceCompany.tblBusiness.BusinessName,
TypeName = insuranceCoverage.InsuranceTypeID,
Address = insuranceCoverage.tblInsuranceCompany.tblBusiness.tblAddresses
.Where(a => a.AddressTypeId = 'b')
.FirstOrDefault()
};
EDIT for further attempt
So I added some associations in the dbml so that I could take Craigs advice below. It almost works. Now I am getting a Cannot resolve symbol
on a.AddressTypeID
. What is strange is that Intellisense tells me that tblAddress
is an EntitySet<tblAdress>
. Am I missing an association or do I have an improper one or am I just TOO many levels deep?
Thoughts?