views:

57

answers:

2

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 tblAddress. I am getting a Cannot resolve method... error. that 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?

+1  A: 

I don't know whether it'll do everything you need, but you can use anonymous types for equijoins with a composite key:

 from x in table1
 join y in table2 on new { x.Id1, x.Id2 } equals new { y.Id1, y.Id2 }
 ...

See if that helps.

Jon Skeet
+1  A: 

It's usually wrong (and way too much work) to use join in LINQ to SQL. Instead, use the navigation/association properties L2S generates for you:

var coverage = 
                from insuranceCoverage in context.tblPersonInsuranceCoverages
                where insuranceCoverage.PersonID == personID
                select new
                    {
                            insuranceCoverage.PersonInsuranceCoverageID,
                            insuranceCoverage.EffectiveDate,
                            insuranceCoverage.ExpirationDate,
                            insuranceCoverage.Priority,
                            CoverageCategory = insuranceCoverage.insuranceCompany.tblAdminInsuranceCompanyType.TypeName,
                            insuranceCoverage.insuranceCompany.tblBusiness.BusinessName,
                            TypeName = insuranceCoverage.InsuranceTypeID,
                            Address = insuranceCoverage.insuranceCompany.Addresses
                                                                        .Where(a => a.AddressTypeID == 'b')
                                                                        .FirstOrDefault()
                        };
Craig Stuntz
I agree, unfortunately I am not being given that option in Intellisense....I wonder why...
Refracted Paladin
Okay, I went into the `dbml` and added some `associations`. I have no idea why they weren't **ALL** generated automatically but I digress. After that I was able to do as you suggested **EXCEPT** for the `ADDRESS` part. See my edit for more details. Thanks!
Refracted Paladin
When you type `a.`, what does IntelliSense show you? You may need to do something like `a.AddressType.Id` (note extra dot).
Craig Stuntz
@Craig Stuntz: Nevermind, I am an idiot. It should have read `a.AddressTypeID == 'b'` Notice the capital **D** and the second **=** wow!! anyway, thanks for the help.
Refracted Paladin